Openmaize v0.16.1 Openmaize.Login

Plug to handle login.

There are four options:

  • redirects - if true, which is the default, redirect on login
  • storage - storage method for the token
  • the default is :cookie
  • if storage is set to nil, redirects is automatically set to false
  • token_validity - length the token is valid for (in minutes)
  • the default is 120 minutes (2 hours)
  • 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.LoginTools 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 sessionStorage to store the token (this will also set redirects to false):

plug Openmaize.Login, [storage: nil] when action in [:login_user]

If you want to use email to identify the user and have the token valid for just two hours:

plug Openmaize.Login, [token_validity: 120, 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.LoginTools.email_username/1] when action in [:login_user]

Summary

Functions

Handle the login POST request

Callback implementation for c:Plug.init/1

Functions

call(conn, arg)

Handle the login POST request.

If the login is successful, a JSON Web Token will be returned.

init(opts)

Callback implementation for c:Plug.init/1.