Skip to content

Instantly share code, notes, and snippets.

@bcardarella
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcardarella/53f6877d3b6613451909 to your computer and use it in GitHub Desktop.
Save bcardarella/53f6877d3b6613451909 to your computer and use it in GitHub Desktop.
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
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