Packages
openmaize
0.10.1
3.0.1
retired
3.0.0
2.9.0
2.8.0
2.7.0
2.6.0
2.5.1
2.5.0
2.4.0
2.3.2
2.3.1
2.3.0
2.2.0
2.1.5
2.1.4
2.1.3
2.1.2
2.1.1
2.1.0
2.0.2
2.0.1
2.0.0
1.0.1
1.0.0
1.0.0-beta.5
1.0.0-beta.4
1.0.0-beta.3
1.0.0-beta.2
1.0.0-beta.1
1.0.0-beta.0
0.19.3
0.19.2
0.19.1
0.19.0
0.18.1
0.18.0
0.17.2
0.17.1
0.17.0
0.16.2
0.16.1
0.16.0
0.15.1
0.15.0
0.14.0
0.13.0
0.12.0
0.11.1
0.11.0
0.10.2
0.10.1
0.10.0
0.8.1
0.8.0
0.7.5
0.7.4
0.7.2
0.7.1
0.7.0
0.6.7
0.6.6
0.6.3
0.6.2
0.6.1
0.6.0
0.5.0
0.4.1
0.4.0
Authentication library for Elixir using Plug
Current section
Files
Jump to
Current section
Files
lib/openmaize/plugs/loginout_check.ex
defmodule Openmaize.LoginoutCheck do
@moduledoc """
Plug to handle login and logout requests.
This module checks if the path is for the login or logout page and handles the
login or logout if necessary. If the path is different, the connection is
returned without any further checks being performed.
If the path ends with `login` and it is a GET request, the current_user
is set to nil and the user is sent straight to the login page. If the path
ends with `login` and it is a POST request, Openmaize processes the login
request and then, if successful, sends a token back to the user, either
stored in a cookie or sent in the response body. If `redirects` is set to
true, the user is redirected to the user's role's page.
If the path ends with `logout` and the token is stored in a cookie, then
the cookie is deleted. If `redirects` is set to true, the user is then
redirected to the home page.
There are three options:
* redirects - if true, which is the default, redirect on login / logout
* storage - storage method for the token -- the default is :cookie
If redirects is set to false, storage is automatically set to nil
* token_validity - length of validity of token (in minutes) -- the default is 1440 minutes (one day)
## Examples
Call LoginoutCheck without any options:
plug Openmaize.LoginoutCheck
Call LoginoutCheck and send the token in the response body:
plug Openmaize.LoginoutCheck, storage: nil
Call LoginoutCheck without redirects:
plug Openmaize.LoginoutCheck, redirects: false
Call LoginoutCheck and set the token validity to two hours:
plug Openmaize.LoginoutCheck, token_validity: 120
"""
import Plug.Conn
alias Openmaize.Login
alias Openmaize.Logout
@behaviour Plug
def init(opts), do: opts
@doc """
Check the path and handle login or logout if necessary. If the path
is not for the login or logout page, the connection is returned.
"""
def call(%Plug.Conn{path_info: path_info} = conn, opts) do
{redirects, storage} = case Keyword.get(opts, :redirects, true) do
true -> {true, Keyword.get(opts, :storage, :cookie)}
false -> {false, nil}
end
token_opts = {0, Keyword.get(opts, :token_validity, 1440)}
case Enum.at(path_info, -1) do
"login" -> handle_login(conn, {redirects, storage, token_opts})
"logout" -> handle_logout(conn, {redirects, storage, token_opts})
_ -> conn
end
end
defp handle_login(%Plug.Conn{method: "POST"} = conn, opts), do: Login.call(conn, opts)
defp handle_login(conn, _opts) do
conn |> assign(:current_user, nil) |> put_private(:openmaize_skip, true)
end
defp handle_logout(conn, opts), do: assign(conn, :current_user, nil) |> Logout.call(opts)
end