Current section

Files

Jump to
phoenix_token_auth lib phoenix_token_auth sessions_controller.ex
Raw

lib/phoenix_token_auth/sessions_controller.ex

defmodule PhoenixTokenAuth.SessionsController do
use Phoenix.Controller
alias PhoenixTokenAuth.Util
alias PhoenixTokenAuth.Authenticator
plug PhoenixTokenAuth.Plug when action in [:delete]
plug :action
@doc """
Log in as an existing user.
Parameter are "email" and "password".
Responds with status 200 and {token: token} if credentials were correct.
Responds with status 401 and {errors: error_message} otherwise.
"""
def create(conn, %{"email" => email, "password" => password}) do
case Authenticator.authenticate(email, password) do
{:ok, token} -> json conn, %{token: token}
{:error, errors} -> Util.send_error(conn, errors, 401)
end
end
@doc """
Destroy the active session.
Will delete the authentication token from the user table.
Responds with status 200 if no error occured.
"""
def delete(conn, _params) do
{:ok, token} = conn
|> Util.token_from_conn
tokens_left_after_delete = conn.assigns.authenticated_user.authentication_tokens
|> List.delete(token)
Util.repo.update %{conn.assigns.authenticated_user | authentication_tokens: tokens_left_after_delete}
json conn, :ok
end
end