Coder Social home page Coder Social logo

sesamex's Introduction

[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.

sesamex's People

Contributors

khusnetdinov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

sesamex's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.