2018-01-13 23:33:03 +01:00
|
|
|
defmodule EventosWeb.UserSessionController do
|
2018-01-14 17:56:50 +01:00
|
|
|
@moduledoc """
|
|
|
|
Controller for user sessions
|
|
|
|
"""
|
2018-01-13 23:33:03 +01:00
|
|
|
use EventosWeb, :controller
|
2018-05-18 09:56:21 +02:00
|
|
|
alias Eventos.Actors.User
|
|
|
|
alias Eventos.Actors
|
2018-01-13 23:33:03 +01:00
|
|
|
|
|
|
|
def sign_in(conn, %{"email" => email, "password" => password}) do
|
2018-07-04 14:29:17 +02:00
|
|
|
with {:ok, %User{} = user} <- Actors.find_by_email(email),
|
|
|
|
{:ok, %User{} = _user} <- User.is_confirmed(user),
|
|
|
|
{:ok, token, _claims} <- Actors.authenticate(%{user: user, password: password}) do
|
2018-01-13 23:33:03 +01:00
|
|
|
# Render the token
|
|
|
|
render conn, "token.json", %{token: token, user: user}
|
2018-07-04 14:29:17 +02:00
|
|
|
else
|
|
|
|
{:error, :not_found} ->
|
|
|
|
conn
|
|
|
|
|> put_status(401)
|
|
|
|
|> json(%{"error_msg" => "No such user", "display_error" => "session.error.bad_login"})
|
|
|
|
{:error, :unconfirmed} ->
|
|
|
|
conn
|
|
|
|
|> put_status(401)
|
|
|
|
|> json(%{"error_msg" => "User is not activated", "display_error" => "session.error.not_activated"})
|
|
|
|
{:error, :unauthorized} ->
|
|
|
|
conn
|
|
|
|
|> put_status(401)
|
|
|
|
|> json(%{"error_msg" => "Bad login", "display_error" => "session.error.bad_login"})
|
2018-01-13 23:33:03 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def sign_out(conn, _params) do
|
|
|
|
conn
|
2018-01-14 17:56:50 +01:00
|
|
|
|> EventosWeb.Guardian.Plug.sign_out()
|
2018-01-13 23:33:03 +01:00
|
|
|
|> send_resp(204, "")
|
|
|
|
end
|
2018-01-14 17:56:50 +01:00
|
|
|
end
|