Created
October 20, 2016 21:47
-
-
Save bnhansn/f45aa93c3faac8fd3f5ab1c9b04b3dc9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Sling.SessionController do | |
| use Sling.Web, :controller | |
| def create(conn, params) do | |
| case authenticate(params) do | |
| {:ok, user} -> | |
| new_conn = Guardian.Plug.api_sign_in(conn, user, :access) | |
| jwt = Guardian.Plug.current_token(new_conn) | |
| new_conn | |
| |> put_status(:created) | |
| |> render("show.json", user: user, jwt: jwt) | |
| :error -> | |
| conn | |
| |> put_status(:unauthorized) | |
| |> render("error.json") | |
| end | |
| end | |
| def delete(conn, _) do | |
| jwt = Guardian.Plug.current_token(conn) | |
| Guardian.revoke!(jwt) | |
| conn | |
| |> put_status(:ok) | |
| |> render("delete.json") | |
| end | |
| def refresh(conn, _params) do | |
| user = Guardian.Plug.current_resource(conn) | |
| jwt = Guardian.Plug.current_token(conn) | |
| {:ok, claims} = Guardian.Plug.claims(conn) | |
| case Guardian.refresh!(jwt, claims, %{ttl: {30, :days}}) do | |
| {:ok, new_jwt, _new_claims} -> | |
| conn | |
| |> put_status(:ok) | |
| |> render("show.json", user: user, jwt: new_jwt) | |
| {:error, _reason} -> | |
| conn | |
| |> put_status(:unauthorized) | |
| |> render("forbidden.json", error: "Not authenticated") | |
| end | |
| end | |
| def unauthenticated(conn, _params) do | |
| conn | |
| |> put_status(:forbidden) | |
| |> render(Sling.SessionView, "forbidden.json", error: "Not Authenticated") | |
| end | |
| defp authenticate(%{"email" => email, "password" => password}) do | |
| user = Repo.get_by(Sling.User, email: String.downcase(email)) | |
| case check_password(user, password) do | |
| true -> {:ok, user} | |
| _ -> :error | |
| end | |
| end | |
| defp check_password(user, password) do | |
| case user do | |
| nil -> Comeonin.Bcrypt.dummy_checkpw() | |
| _ -> Comeonin.Bcrypt.checkpw(password, user.password_hash) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment