Skip to content

Instantly share code, notes, and snippets.

@motephyr
Created September 11, 2017 17:31
Show Gist options
  • Save motephyr/0f7bcc10a218365c6d8d85d6109cf1b5 to your computer and use it in GitHub Desktop.
Save motephyr/0f7bcc10a218365c6d8d85d6109cf1b5 to your computer and use it in GitHub Desktop.
#...
@spec init(Keyword.t) :: [tuple]
def init(opts) do
login =
case opts[:login] do
true -> true
fun when is_function(fun) ->
fun
other ->
case opts[:protected] do
nil -> other
true -> true
other -> other
end
end
rememberable? =
if Config.has_option(:rememberable) do
Config.user_schema.rememberable?
else
false
end
%{
login: login,
error: Keyword.get(opts, :error, "HTTP Authentication Required"),
db_model: Keyword.get(opts, :db_model),
id_key: Keyword.get(opts, :id, :id),
store: Keyword.get(opts, :store, Coherence.CredentialStore.Session),
assigns_key: Keyword.get(opts, :assigns_key, :current_user),
login_key: Keyword.get(opts, :login_cookie, Config.login_cookie),
rememberable: Keyword.get(opts, :rememberable, rememberable?),
cookie_expire: Keyword.get(opts, :login_cookie_expire_hours, Config.rememberable_cookie_expire_hours) * 60 * 60,
rememberable_callback: Keyword.get(opts, :rememberable_callback)
}
end
@doc false
@spec call(conn, Keyword.t) :: conn
def call(conn, opts) do
if get_authenticated_user(conn) do
conn
else
conn
|> get_session_data
|> verify_auth_key(opts, opts[:store])
|> verify_rememberable(opts)
|> assert_login(opts[:login], opts)
end
end
#...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment