ProductPromotion
Logo

Elixir

made by https://0x3d.site

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 Site

GitHub - khusnetdinov/sesamex: [WIP] Another authentication hex for Phoenix.

GitHub - khusnetdinov/sesamex: [WIP] Another authentication hex for Phoenix.

[WIP] Sesamex Code Triagers Badge Build Status Ebert Hex.pm

img

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.

  1. Create model and migration for User.
$ mix sesamex.gen.model User users
  1. Migrate database.
$ mix ecto.migrate
  1. Create predefined controllers for using them in authentication.
$ mix sesamex.gen.controllers User
  1. Create predefined views modules.
$ mix sesamex.gen.views User
  1. Create predefined templates for sign_up and sign_in pages.
$ mix sesamex.gen.templates User
  1. Add routes modules Sesamex.Routes, Sesamex.Pipeline and functions authenticate, session.
$ mix sesamex.gen.routes User
  1. Add routes functions to layout.html.eex, remove lines with Get 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 %>
  1. 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.

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory