Last active
January 19, 2020 15:58
-
-
Save 8zca/7953e2031f1896507a0d5c259102155c to your computer and use it in GitHub Desktop.
lib/phoenix_admin_web/controllers/session_controller.ex
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
defmodule PhoenixAdminWeb.SessionController do | |
use PhoenixAdminWeb, :controller | |
alias PhoenixAdmin.Accounts | |
alias PhoenixAdmin.Accounts.User | |
def new(conn, _) do | |
changeset = Accounts.change_user(%User{}) | |
conn | |
|> render("new.html", changeset: changeset) | |
end | |
def login(conn, %{"user" => %{"email" => email, "password" => password}}) do | |
auth_result = Accounts.authenticate_user(email, password) | |
case auth_result do | |
{:ok, user} -> | |
conn | |
|> put_flash(:info, "Welcome back!") | |
|> Guardian.Plug.sign_in(PhoenixAdmin.Accounts.Guardian, user) | |
|> redirect(to: Routes.user_path(conn, :index)) | |
{:error, reason} -> | |
conn | |
|> put_flash(:error, to_string(reason)) | |
|> new(%{}) | |
end | |
end | |
def logout(conn, _) do | |
conn | |
|> Guardian.Plug.sign_out(PhoenixAdmin.Accounts.Guardian, []) | |
|> redirect(to: Routes.session_path(conn, :login)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment