secure_password v0.6.0 SecurePassword

Provides an easy way to interact with hashed password for Ecto models.

Example

defmodule User do
  use Ecto.Schema
  use SecurePassword

  import Ecto.Changeset

  schema "users" do
    field :email, :string
    field :name, :string

    has_secure_password
  end

  @required_fields ~w(email)
  @optional_fields ~w(name password)

  def changeset(model, params \\ :invalid) do
    model
    |> cast(params, @required_fields, @optional_fields)
    |> with_secure_password(min_length: 8)
  end
end

Summary

Functions

Checks if the model password if valid

Validates and modify the changeset to remove the password and password_confirmation fields and add the hashed password_digest field

Macros

Defines the needed fiels in the schema. Expects password_digest row to exist in the database

Functions

authenticate(model, password)

Checks if the model password if valid.

with_secure_password(changeset, opts \\ [])

Validates and modify the changeset to remove the password and password_confirmation fields and add the hashed password_digest field.

Options

  • required: Do not force the password to be present if set to false. (default: true)
  • min_length: Set the minimum length for the password. (default: 6)

Macros

has_secure_password()

Defines the needed fiels in the schema. Expects password_digest row to exist in the database.

schema "user" do
    field "name", :string

    has_secure_password
end