Skip to content

User authentication library with mail verification that doesn't require Phoenix

Notifications You must be signed in to change notification settings

ZuraGuerra/elegua

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Elegua

TODO: Add description

Setup

Add your model specifications to config.

config :elegua, # These are the default values, but imagine a model like:
  user_model: YourApp.User, # MyApp.Player
  app_repo: YourApp.Repo, # MyApp.Repo
  password_field: :password, # :passphrase
  username_field: :username, # :user
  email_field: :email, # :mail
  # these two are only needed if you'd like to verify users' email
  # and thi is the only field that must have this name
  verification_token_field: :verification_token,
  is_verified_field: :is_verified # :active

Use

Registration

Simple DB creation

defmodule MyApp.Registration do
  alias MyApp.MyUser

  def create(params) do
    changeset =
      Elegua.changeset(%MyUser{}, params)
      |> MyUser.another_changeset(params)
      # And so on ad infinitum

    if changeset.valid? do
      {:ok, user} = Elegua.register(changeset)
      # Other happy things
    else
      # Handle error
    end

  end
end

Email verification

defmodule MyApp.Registration do
  alias MyApp.MyUser

  @from "no-reply@myawesomeapp.com"
  @subject "Welcome to MyAwesomeApp!"

  def create(params) do
    changeset =
      Elegua.changeset(%MyUser{}, params)
      |> MyUser.another_changeset(params)

    if changeset.valid? do
      # Passing `verify` as an option
      {:ok, user} = Elegua.register(changeset, :verify)
      verification_token = changeset.params["verification_token"]
      
      # You must add the token to the content
      content = "Your verification token: #{verification_token}"
      Elegua.send_verification_email(user_email, @from, @subject, {:text, content})
      # OR you can send HTML
      # content = "<h2>Welcome to Phoenix!</h2>"
      # Elegua.send_verification_email(user_email, @from, @subject, {:html, content})

      # IF you're using Phoenix, you can get your templates
      # rendered to string with `Phoenix.View.render_to_string`
      # https://hexdocs.pm/phoenix/Phoenix.View.html#render_to_string/3
    else
      # Handle error
    end
  end

  def verify(token) do
    Elegua.verify(token)
  end
end

Installation

If available in Hex, the package can be installed as:

  1. Add elegua to your list of dependencies in mix.exs:

    def deps do [{:elegua, "~> 0.0.1"}] end

  2. Ensure elegua is started before your application:

    def application do [applications: [:elegua]] end

About

User authentication library with mail verification that doesn't require Phoenix

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages