Product Promotion
0x5a.live
for different kinds of informations and explorations.
GitHub - khusnetdinov/sesamex: [WIP] Another authentication hex for Phoenix.
[WIP] Another authentication hex for Phoenix. Contribute to khusnetdinov/sesamex development by creating an account on GitHub.
Visit SiteGitHub - khusnetdinov/sesamex: [WIP] Another authentication hex for Phoenix.
[WIP] Another authentication hex for Phoenix. Contribute to khusnetdinov/sesamex development by creating an account on GitHub.
Powered by 0x5a.live 💗
[WIP] Sesamex
NOTE: This repo in active development!
Getting started
Sesamex is a simple and flexible authentication solution for Elixir / Phoenix.
Implemented functionality:
:registration
:session
Todo functionality:
:remember
:track
- email adapter
- email in browser
:confirm
:recover
:invite
:lock, :unlock
- token auth
:oauth
- change layout
Installation
Add sesamex
to your list of dependencies in mix.exs
:
def deps do
[{:sesamex, "~> 0.2.1"}]
end
Authentication in 5 minutes!
We 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.Pipeline
and functionsauthenticate
,session
.
$ mix sesamex.gen.routes User
- Add routes functions to
layout.html.eex
, remove lines withGet Started
link 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
.
Usage
Sesamex goes with helper modules which keep required logic for authentification.
Generate model and migration for authentication process:
$ mix sesamex.gen.model Model models
The tasks accepts the same argements as phoenix model generation task - singular and plural forms of model. Task will generate 2 files:
web/models/model.ex
priv/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.
Generate predefined controllers modules for model scope:
$ mix sesamex.gen.controllers Model
Task 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.
Generation predefined views modules for model scope:
$ mix sesamex.gen.views Model
Task do the job like controllers task for views.
Generation predefined templates for sign_up
, sign_in
pages:
$ mix sesames.gen.templates Model
Task get Model
scope name and create templates for pages:
web/templates/model/registration/new.html.eex
web/templates/model/session/new.html.eex
Templates file keep predefined eex markup for using pages instantly.
Generation routes and define default controllers:
$ mix sesamex.gen.routes Model
Adds 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
# ...
end
Module 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
# ...
Generation current model assigns in @conn:
Sesamex add to @conn current_model
assigns for Model
. You can use @corrent_model
in views for checking persistance of authenticated model.
License
The gem is available as open source under the terms of the MIT License.
Elixir Resources
are all listed below.
Made with ❤️
to provide different kinds of informations and resources.