Openmaize v0.19.2 Openmaize.DB
Functions to help with interacting with Ecto when using Openmaize.
Creating a custom database module
This is the default database module, but you can use a custom module
by changing the db_module
value in the config file.
If you are going to create a custom module, note that the following functions are called by other modules in Openmaize:
find_user
- used in Openmaize.Login and Openmaize.ConfirmEmailuser_confirmed
- used in Openmaize.ConfirmEmailpassword_reset
- used in Openmaize.ResetPasswordcheck_time
- used in Openmaize.ConfirmEmail and Openmaize.ResetPassword
User model
The example schema below is the most basic setup for Openmaize (:username and :password_hash are configurable):
schema "users" do
field :username, :string
field :role, :string
field :password, :string, virtual: true
field :password_hash, :string
timestamps
end
In the example above, the :username
is used to identify the user. This can
be set to any other value, such as :email
. See the documentation for
Openmaize.Login for details about logging in with a different value.
See the documentation for Openmaize.Config for details about configuring
the :password_hash
value.
The :role
is needed for authorization, and the :password
and the
:password_hash
fields are needed for the add_password_hash
function
in this module. Note the addition of virtual: true
to the definition
of the password field. This means that it will not be stored in the database.
Summary
Functions
Add a confirmation token to the user model or changeset
Hash the password and add it to the user model or changeset
Add a reset token to the user model or changeset
Function used to check if a token has expired
Find the user in the database
Find the user, using the user id, in the database
Add the password hash for the new password to the database
Change the confirmed_at
value in the database to the current time
Functions
Add a confirmation token to the user model or changeset.
Add the following three entries to your user schema:
field :confirmation_token, :string
field :confirmation_sent_at, Ecto.DateTime
field :confirmed_at, Ecto.DateTime
Examples
In the following example, the add_confirm_token
function is called with
a key generated by Openmaize.ConfirmEmail.gen_token_link
:
changeset
|> Openmaize.DB.add_confirm_token(key)
Hash the password and add it to the user model or changeset.
Before the password is hashed, it is checked to make sure that it is not too weak. See the documentation for the Openmaize.Password module for more information about the options available.
This function will return a changeset. If there are any errors, they will be added to the changeset.
Comeonin.Bcrypt is the default hashing function, but this can be changed to Comeonin.Pbkdf2, or any other algorithm, by setting the Config.crypto_mod value.
Add a reset token to the user model or changeset.
Add the following two entries to your user schema:
field :reset_token, :string
field :reset_sent_at, Ecto.DateTime
As with add_confirm_token
, the function Openmaize.ConfirmEmail.gen_token_link
can be used to generate the token and link.
Add the password hash for the new password to the database.
If the update is successful, the reset_token and reset_sent_at values will be set to nil.