Openmaize.Authenticate
Plug to authenticate users, using Json Web Tokens.
For more information about Json Web Tokens, see the documentation for the Openmaize.Token module.
This module also sets the current_user variable, which, if you are using Phoenix, can then be used in your templates. If no token is found, the current_user is set to nil.
There are two 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
Examples
Call Authenticate without any options:
Plug Openmaize.Authenticate
Call Authenticate and send the token in the response body:
Plug Openmaize.Authenticate, storage: nil
Call Authenticate without redirects:
Plug Openmaize.Authenticate, redirects: false
Summary↑
call(conn, opts) | This function checks the token, which is either in a cookie or the request headers and authenticates the user based on the information in the token |
init(opts) | Callback implementation for |
Functions
This function checks the token, which is either in a cookie or the request headers and authenticates the user based on the information in the token.
If the authentication is successful, a map, called :current_user
,
providing the user information is added to the assigns
map in the
Plug connection. If there is no token, the :current_user
is set to nil.
If there is an error, the user is either redirected to the login page or an error message is sent to the user. The connection is also halted.
Callback implementation for c:Plug.init/1
.