Openmaize v0.16.1 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.Confirm
  • user_confirmed - used in Openmaize.Confirm
  • password_reset - used in Openmaize.Confirm
  • check_time - used in Openmaize.Confirm

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 (see the documentation for Openmaize.Config for information about changing :password_hash to some other value). 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 and update the database

Function used to check if a token has expired

Find the user 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_confirm_token(user, key)

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.ConfirmTools.gen_token_link:

changeset
|> Openmaize.DB.add_confirm_token(key)
add_password_hash(user, params)

Hash the password and add it to the user model or changeset.

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 by setting the Config.get_crypto_mod value to :pbkdf2.

add_reset_token(user, key)

Add a reset token to the user model and update the database.

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.ConfirmTools.gen_token_link can be used to generate the token and link.

check_time(sent_at, valid_secs)

Function used to check if a token has expired.

find_user(user_id, uniq)

Find the user in the database.

password_reset(user, password)

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.

This function is used by the Openmaize.Confirm module.

user_confirmed(user)

Change the confirmed_at value in the database to the current time.