Openmaize v2.0.0 Openmaize.Login
Module to handle login.
There are two options:
db_module - the module that is used to query the database
- in most cases, this will be generated by
mix openmaize.gen.ectodb
and will be called MyApp.OpenmaizeEcto - if you implement your own database module, it needs to implement the Openmaize.Database behaviour
- in most cases, this will be generated by
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
- the default is
Examples with Phoenix
Replace MyApp with the name of your application in the examples below.
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, [db_module: MyApp.OpenmaizeEcto] when action in [:login_user]
If you want to use email
to identify the user:
plug Openmaize.Login, [db_module: MyApp.OpenmaizeEcto,
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, [db_module: MyApp.OpenmaizeEcto,
unique_id: &Openmaize.Login.Name.email_username/1] when action in [:login_user]
Summary
Functions
Handle the login POST request.
If the login is successful and otp_required: true
is not in the
user model, the user will be added to the conn.private.openmaize_user
value. You can then use the information in the user model to add
the user to the session.
If the login is unsuccessful, an error message will be added to
conn.private.openmaize_error
.
Two factor authentication
If otp_required: true
is in the user model and if the login is
successful, conn.private.openmaize_otpdata
will be set to the
user id.
User email confirmation
Before checking the password, the user struct will be checked for a
confirmed_at
value. If it is set to nil, an error message will be
added to conn.private.openmaize_error
.