Packages
openmaize
0.19.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/login.ex
defmodule Openmaize.Login do
@moduledoc """
Module to handle login.
There are four options:
* storage - store the token in a cookie, which is the default, or not have Openmaize handle the storage
* if you are developing an api or want to store the token in sessionStorage, set storage to nil
* unique_id - the name which is used to identify the user (in the database)
* the default is `:username`
* this can also be a function which checks the user input and returns an atom
* see the Openmaize.Login.Name module for some example functions
* add_jwt - the function used to add the JSON Web Token to the response
* the default is `&OpenmaizeJWT.Plug.add_token/5`
* override_exp - set the default number of minutes that a token is valid for (overriding the default)
* the default token validity is set in the OpenmaizeJWT config
* the default is nil (no override)
## Remember me
By using the `override_exp` option, you can override the default token
validity on a case-by-case basis. This can help you implement a `remember
me` option on the login page.
It is recommended that `override_exp` is not set too high (in the example
below, it is set to 10_080 [7 days]). In addition, it should not be used
when protecting high, or even medium, value resources.
## Examples with Phoenix
If you have used the `mix openmaize.gen.phoenixauth` command to generate
an Authorize module, the `login_user` function in the examples below
will simply call the `Authorize.handle_login` function.
In the `web/router.ex` file, add the following line (you can use
a different controller and route):
post "/login", PageController, :login_user
And then in the `page_controller.ex` file, add:
plug Openmaize.Login when action in [:login_user]
If you want to use `email` to identify the user:
plug Openmaize.Login, [unique_id: :email] when action in [:login_user]
If you want to use `email` or `username` to identify the user (allowing the
end user a choice):
plug Openmaize.Login, [unique_id: &Openmaize.Login.Name.email_username/1] when action in [:login_user]
And if you want to override the default value for token validity, to
implement a 'remember me' functionality, for example:
plug Openmaize.Login, [override_exp: 10_080] when action in [:login_user]
The above command creates a token that is valid for 7 days (10080 minutes)
if "remember_me" in the user_params is set to true.
"""
use Openmaize.Login.Base
end