Openmaize v0.17.1 Openmaize.Login
Plug to handle login.
There are two 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
Examples with Phoenix
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 the login_user
function could be written like this:
def login_user(%Plug.Conn{private: %{openmaize_error: message}} = conn, _opts) do
conn
|> put_flash(:error, message)
|> redirect(to: page_path(conn, :index)
end
def login_user(%Plug.Conn{private: %{openmaize_info: message}} = conn, _opts) do
conn
|> put_flash(:info, message)
|> redirect(to: user_path(conn, :index)
end
Summary
Functions
Handle the login POST request.
If the login is successful, a JSON Web Token will be returned.
The JWT will be either stored in a cookie, or it will be returned in the body of the response.