-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from camino-school/better-login-flow
Better login flow
- Loading branch information
Showing
36 changed files
with
1,724 additions
and
1,369 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,6 +131,26 @@ defmodule Lanttern.Identity do | |
User.email_changeset(user, attrs, validate_email: false) | ||
end | ||
|
||
@doc """ | ||
Updates an user email. | ||
This function should be use only in admin (and maybe it should be removed | ||
or refactored in the near future, adding more security guards). | ||
## Examples | ||
iex> admin_update_user_email(user, %{"email => "[email protected]"}) | ||
{:ok, %User{}} | ||
iex> admin_update_user_email(user, %{"email => "[email protected]"}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def admin_update_user_email(user, params) do | ||
user | ||
|> User.email_changeset(params) | ||
|> Repo.update() | ||
end | ||
|
||
@doc """ | ||
Emulates that the email will change without actually changing | ||
it in the database. | ||
|
@@ -422,6 +442,25 @@ defmodule Lanttern.Identity do | |
end | ||
end | ||
|
||
@doc """ | ||
Deletes a user. | ||
This function should be used only in admin (and maybe it should be removed | ||
or refactored in the near future, adding more security guards). | ||
## Examples | ||
iex> admin_delete_user(user) | ||
{:ok, %User{}} | ||
iex> admin_delete_user(user) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def admin_delete_user(%User{} = user) do | ||
Repo.delete(user) | ||
end | ||
|
||
@doc """ | ||
Returns the list of profiles. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
defmodule LantternWeb.UserController do | ||
use LantternWeb, :controller | ||
|
||
alias Lanttern.Identity | ||
alias Lanttern.Identity.User | ||
|
||
def index(conn, _params) do | ||
users = Identity.list_users() | ||
render(conn, :index, users: users) | ||
end | ||
|
||
def new(conn, _params) do | ||
changeset = Identity.change_user_email(%User{}) | ||
|
||
render(conn, :new, changeset: changeset) | ||
end | ||
|
||
def create(conn, %{"user" => user_params}) do | ||
# the form doesn't have the password field because we're only using | ||
# Google Sign In, but we need a password in order to create the user | ||
user_params = Map.put(user_params, "password", Ecto.UUID.generate()) | ||
|
||
case Identity.register_user(user_params) do | ||
{:ok, user} -> | ||
conn | ||
|> put_flash(:info, "User created successfully.") | ||
|> redirect(to: ~p"/admin/users/#{user}") | ||
|
||
{:error, %Ecto.Changeset{} = changeset} -> | ||
render(conn, :new, changeset: changeset) | ||
end | ||
end | ||
|
||
def show(conn, %{"id" => id}) do | ||
user = Identity.get_user!(id) | ||
render(conn, :show, user: user) | ||
end | ||
|
||
def edit(conn, %{"id" => id}) do | ||
user = Identity.get_user!(id) | ||
changeset = Identity.change_user_email(user) | ||
|
||
render(conn, :edit, user: user, changeset: changeset) | ||
end | ||
|
||
def update(conn, %{"id" => id, "user" => user_params}) do | ||
user = Identity.get_user!(id) | ||
|
||
case Identity.admin_update_user_email(user, user_params) do | ||
{:ok, user} -> | ||
conn | ||
|> put_flash(:info, "User updated successfully.") | ||
|> redirect(to: ~p"/admin/users/#{user}") | ||
|
||
{:error, %Ecto.Changeset{} = changeset} -> | ||
render(conn, :edit, user: user, changeset: changeset) | ||
end | ||
end | ||
|
||
def delete(conn, %{"id" => id}) do | ||
user = Identity.get_user!(id) | ||
{:ok, _user} = Identity.admin_delete_user(user) | ||
|
||
conn | ||
|> put_flash(:info, "User deleted successfully.") | ||
|> redirect(to: ~p"/admin/users") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule LantternWeb.UserHTML do | ||
use LantternWeb, :html | ||
|
||
embed_templates "user_html/*" | ||
|
||
@doc """ | ||
Renders a user form. | ||
""" | ||
attr :changeset, Ecto.Changeset, required: true | ||
attr :action, :string, required: true | ||
|
||
def user_form(assigns) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<.header> | ||
Edit User <%= @user.id %> | ||
<:subtitle>Use this form to manage user records in your database.</:subtitle> | ||
</.header> | ||
|
||
<.user_form changeset={@changeset} action={~p"/admin/users/#{@user}"} /> | ||
|
||
<.back navigate={~p"/admin/users"}>Back to users</.back> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<.header> | ||
Listing Users | ||
<:actions> | ||
<.link href={~p"/admin/users/new"}> | ||
<.button>New User</.button> | ||
</.link> | ||
</:actions> | ||
</.header> | ||
|
||
<.table id="users" rows={@users} row_click={&JS.navigate(~p"/admin/users/#{&1}")}> | ||
<:col :let={user} label="User"><%= user.email %></:col> | ||
<:action :let={user}> | ||
<div class="sr-only"> | ||
<.link navigate={~p"/admin/users/#{user}"}>Show</.link> | ||
</div> | ||
<.link navigate={~p"/admin/users/#{user}/edit"}>Edit</.link> | ||
</:action> | ||
<:action :let={user}> | ||
<.link href={~p"/admin/users/#{user}"} method="delete" data-confirm="Are you sure?"> | ||
Delete | ||
</.link> | ||
</:action> | ||
</.table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<.header> | ||
New User | ||
<:subtitle>Use this form to manage user records in your database.</:subtitle> | ||
</.header> | ||
|
||
<.user_form changeset={@changeset} action={~p"/admin/users"} /> | ||
|
||
<.back navigate={~p"/admin/users"}>Back to users</.back> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<.header> | ||
User <%= @user.id %> | ||
<:subtitle>This is a user record from your database.</:subtitle> | ||
<:actions> | ||
<.link href={~p"/admin/users/#{@user}/edit"}> | ||
<.button>Edit user</.button> | ||
</.link> | ||
</:actions> | ||
</.header> | ||
|
||
<.list> | ||
<:item title="User"><%= @user.email %></:item> | ||
</.list> | ||
|
||
<.back navigate={~p"/admin/users"}>Back to users</.back> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<.simple_form :let={f} for={@changeset} action={@action}> | ||
<.error :if={@changeset.action}> | ||
Oops, something went wrong! Please check the errors below. | ||
</.error> | ||
<.input field={f[:email]} type="text" label="Email" /> | ||
<:actions> | ||
<.button>Save User</.button> | ||
</:actions> | ||
</.simple_form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.