Last active
August 29, 2015 14:27
-
-
Save bcardarella/53f6877d3b6613451909 to your computer and use it in GitHub Desktop.
This file contains 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 MyApp.Authenticator do | |
import Plug.Conn | |
def attempt_to_authenticate(conn, _) do | |
case find_account(conn) do | |
{ :ok, account } -> | |
assign(conn, :account, account) | |
:error -> | |
conn | |
|> send_resp(401, "") | |
|> halt | |
end | |
end | |
defp find_account(conn) do | |
account_id = conn | |
|> fetch_session | |
|> get_session(:account_id) | |
case MyApp.Repo.get(MyApp.Account, account_id) do | |
nil -> | |
:error | |
account -> | |
{ :ok, account } | |
end | |
end | |
end |
This file contains 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 MyApp.Route do | |
pipeline :authenticated do | |
plug :authenticated | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment