Skip to content

Instantly share code, notes, and snippets.

@bcardarella
Created August 5, 2015 13:19
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/6cd6854b2d3a1829b537 to your computer and use it in GitHub Desktop.
Save bcardarella/6cd6854b2d3a1829b537 to your computer and use it in GitHub Desktop.
defmodule SogApi.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 SogApi.Repo.get(SogApi.Account, account_id) do
nil ->
:error
account ->
{ :ok, account }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment