Haytni

Haytni is a configurable authentication system for Phoenix, inspired (and yet the word is weak) by Devise (should be almost compatible with it).

Goals:

Plugins:

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/haytni.

Installation

If available in Hex, the package can be installed by adding haytni to your list of dependencies in mix.exs:

def deps do
[
{:haytni, "~> 0.0.2"},
# ...
]
end

Run mix deps.get.

Configure Haytni your_app/config/config.exs

config :haytni,
repo: YourApp.Repo,
schema: YourApp.User,
#mailer: YourApp.Mailer # see below

These are the mandatory options. See options of each plugin for full customizations.

Run mix haytni.install which has the following options (command arguments):

Change your_app/lib/your_app_web/router.ex

defmodule YourAppWeb.Router do
use YourAppWeb, :router
require Haytni # <= add this line
# ...
pipeline :browser do
# ...
plug Haytni.CurrentUserPlug # <= add this line
end
scope "/" do
# ...
Haytni.routes() # <= add this line
end
# ...
end

Change your_app/lib/your_app/user.ex

defmodule YouApp.User do
require Haytni # <= add this line
# ...
schema "..." do
# ...
Haytni.fields() # <= add this line
end
# ...
end

Emails

For plugins which send emails (Confirmable, Lockable and Recoverable):

Create your_app/lib/mailer.ex as follows:

defmodule YourApp.Mailer do
use Bamboo.Mailer, otp_app: :your_app
def from, do: {"mydomain.com", "noreply.mydomain.com"}
end

Add to your_app/lib/your_app_web/router.ex

if Mix.env() == :dev do
Application.ensure_started(:bamboo)
if Version.compare(Application.spec(:bamboo, :vsn) |> to_string, "0.8.0") == :lt do
# Bamboo > 0.8
forward "/sent_emails", Bamboo.SentEmailViewerPlug
else
# Bamboo <= 0.8
forward "/sent_emails", Bamboo.EmailPreviewPlug
end
end

Configure email sending in your_app/config/dev.exs:

config :yourapp, YourApp.Mailer,
adapter: Bamboo.LocalAdapter
config :haytni,
mailer: YourApp.Mailer # <= add/change this line

For production (your_app/config/prod.exs): see Bamboo's documentation

General configuration:

Warning: plugins order matters (in some case). Example: for correct handling of "automatic" authentification (find_user callback), Authenticable must appears before Rememberable in order to give precedence to the current session on the remember me cookie.

Plugins

Authenticable

Fields:

Configuration:

Routes:

Registerable

Change your_app/lib/your_app/user.ex

defmodule YourApp.User do
# ...
@attributes ~W[email password]a # add any field you'll may need
# called when a user try to register himself
def create_registration_changeset(%__MODULE__{} = struct, params) do
struct
|> cast(params, @attributes)
|> validate_required(@attributes)
# add any custom validation here
|> Haytni.validate_create_registration()
end
# called when a user try to edit its own account (logic is completely different from registration)
def update_registration_changeset(%__MODULE__{} = struct, params) do
struct
|> cast(params, ~W[email password current_password]a)
# /!\ email and password are not necessarily required here /!\
# add any custom validation here
|> Haytni.validate_update_registration()
end
# ...
end

Configuration:

Routes:

Rememberable

Fields:

Configuration:

Routes: none

Confirmable

Fields:

Configuration:

Routes:

Recoverable

Fields:

Configuration:

Routes:

Lockable

Fields:

Configuration:

Routes:

Quick recap

Functions you have to implement: