Sesamex is a simple and flexible authentication solution for Elixir / Phoenix.
:registration:session
:remember:track- email adapter
- email in browser
:confirm:recover:invite:lock, :unlock- token auth
:oauth- change layout
Add sesamex to your list of dependencies in mix.exs:
def deps do
[{:sesamex, "~> 0.2.1"}]
endWe are going to create authentication for User model in 5 minutes with phoenix.new bootstrapped application.
- Create model and migration for
User.
$ mix sesamex.gen.model User users- Migrate database.
$ mix ecto.migrate- Create predefined controllers for using them in authentication.
$ mix sesamex.gen.controllers User- Create predefined views modules.
$ mix sesamex.gen.views User- Create predefined templates for sign_up and sign_in pages.
$ mix sesamex.gen.templates User- Add routes modules
Sesamex.Routes,Sesamex.Pipelineand functionsauthenticate,session.
$ mix sesamex.gen.routes User- Add routes functions to
layout.html.eex, remove lines withGet Startedlink and paste code below.
<%= if @current_user do %>
<li><%= @current_user.email %></li>
<li><%= link "sign_out", to: session_path(@conn, :delete), method: "delete" %>
<% else %>
<li><%= link "sign_up", to: registration_path(@conn, :new) %></li>
<li><%= link "sign_in", to: session_path(@conn, :new) %></li>
<% end %>- Run server
mix phoenix.server.
Sesamex goes with helper modules which keep required logic for authentification.
$ mix sesamex.gen.model Model modelsThe tasks accepts the same argements as phoenix model generation task - singular and plural forms of model. Task will generate 2 files:
web/models/model.expriv/repo/migrations/#{timestamp}_create_model.ex
Model file keeps schema and changesets functions which were taken from Sesamex.Model by default. If you want customize, just write you own logic.
By default Sesamex use email, password fields for authentication. If you want to change them or add additional fields you need to change migration and schema in this files.
$ mix sesamex.gen.controllers ModelTask get Model in singular form for defining controllers modules scope and generate files. In example:
├── controllers/
│ ├── model/
│ │ ...
│ │ └── registration_controller.ex
│ └── page_controller.ex
By defualt sesamex generate controllers with scope Project.Model.ExampleController. If you want to use custom controllers you need change settings in routes. See below.
$ mix sesamex.gen.views ModelTask do the job like controllers task for views.
$ mix sesames.gen.templates ModelTask get Model scope name and create templates for pages:
web/templates/model/registration/new.html.eexweb/templates/model/session/new.html.eex
Templates file keep predefined eex markup for using pages instantly.
$ mix sesamex.gen.routes ModelAdds to web/routex.ex Sesamex modules and helpers:
defmodule Project.Routes do
# ...
use Sesamex.Routes
use Sesamex.Pipeline
alias Project.Repo
alias Project.User
pipeline :browser do
# ...
plug :session, [:user, User, Repo]
end
scope "/", Project do
# ...
authenticate :users
get "/", PageController, :index
end
# ...
endModule Sesamex.Routes macro authenticate: 2 keep logic for generation route_paths for model. Note that you should atom in plural form :models. There are 2 opts only, except, see examples:
only: [:module, :other_module]except: [:module, :other_module]
By default macros generate routes for controllers which shoud be scoped by model name, see example:
authenticate :users, only: [:registration]
# Generate routes
# registration_path GET /users/sign_up Project.User.RegistrationController :new
# ...If you want to redifine controller name, use controllers Keywords list:
- controllers: [module: OtherController] - Redefine controllers for module.
authenticate :users, controllers: [registration: OtherController]
# Generate routes
# registration_path GET /users/sign_up Project.OtherController :new
# ...Sesamex add to @conn current_model assigns for Model. You can use @corrent_model in views for checking persistance of authenticated model.
The gem is available as open source under the terms of the MIT License.
