2017-12-09 14:58:37 +01:00
|
|
|
defmodule EventosWeb.SessionController do
|
|
|
|
use EventosWeb, :controller
|
|
|
|
alias Eventos.Accounts.User
|
|
|
|
alias Eventos.Accounts
|
|
|
|
|
|
|
|
def sign_in(conn, %{"email" => email, "password" => password}) do
|
|
|
|
with %User{} = user <- Accounts.find(email) do
|
|
|
|
# Attempt to authenticate the user
|
|
|
|
with {:ok, token, _claims} <- Accounts.authenticate(%{user: user, password: password}) do
|
|
|
|
# Render the token
|
2018-01-09 17:52:26 +01:00
|
|
|
user = Eventos.Repo.preload user, :account
|
|
|
|
render conn, "token.json", %{token: token, user: user}
|
2017-12-09 14:58:37 +01:00
|
|
|
end
|
2018-01-09 17:52:26 +01:00
|
|
|
send_resp(conn, 400, "Bad login")
|
2017-12-09 14:58:37 +01:00
|
|
|
end
|
2018-01-09 17:52:26 +01:00
|
|
|
send_resp(conn, 400, "No such user")
|
2017-12-09 14:58:37 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def sign_out(conn, _params) do
|
|
|
|
conn
|
|
|
|
|> Eventos.Guardian.Plug.sign_out()
|
|
|
|
|> send_resp(204, "")
|
|
|
|
end
|
|
|
|
|
|
|
|
def show(conn, _params) do
|
|
|
|
user = Eventos.Guardian.Plug.current_resource(conn)
|
|
|
|
|
|
|
|
send_resp(conn, 200, Poison.encode!(%{user: user}))
|
|
|
|
end
|
|
|
|
end
|