Created
June 5, 2016 02:02
-
-
Save bratsche/7f23f9eba13dd300ef4d65445aefc152 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 Foo.SessionController do | |
use Foo.Web, :controller | |
def create(conn, %{"session" => session_params}) do | |
case Foo.Session.authenticate(session_params) do | |
{:ok, user} -> | |
{:ok, jwt, _full_claims} = Guardian.encode_and_sign(user, :token) | |
conn | |
|> put_status(:created) | |
|> render("show.json", jwt: jwt, user: user) | |
:error -> | |
conn | |
|> put_status(:unprocessable_entity) | |
|> render("error.json") | |
end | |
end | |
def unauthenticated(conn, _params) do | |
conn | |
|> put_status(:forbidden) | |
|> render(Foo.SessionView, "forbidden.json", error: "Not Authenticated") | |
end | |
end | |
defmodule Foo.SomeOtherController do | |
use Foo.Web, :controller | |
plug Guardian.Plug.EnsureAuthenticated, handler: Foo.SessionController | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment