2018-10-11 17:37:39 +02:00
|
|
|
defmodule MobilizonWeb.UserSessionController do
|
2018-01-14 17:56:50 +01:00
|
|
|
@moduledoc """
|
|
|
|
Controller for user sessions
|
|
|
|
"""
|
2018-10-11 17:37:39 +02:00
|
|
|
use MobilizonWeb, :controller
|
|
|
|
alias Mobilizon.Actors.User
|
|
|
|
alias Mobilizon.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-07-27 10:45:35 +02: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"})
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-07-04 14:29:17 +02:00
|
|
|
{:error, :unconfirmed} ->
|
|
|
|
conn
|
|
|
|
|> put_status(401)
|
2018-07-27 10:45:35 +02:00
|
|
|
|> json(%{
|
|
|
|
"error_msg" => "User is not activated",
|
|
|
|
"display_error" => "session.error.not_activated"
|
|
|
|
})
|
|
|
|
|
2018-07-04 14:29:17 +02:00
|
|
|
{: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-10-11 17:37:39 +02:00
|
|
|
|> MobilizonWeb.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
|