gandalf v0.2.0 Gandalf.Plug.Authenticate

Gandalf plug implementation to check authentications and to set resouce owner.

Summary

Functions

Plug function to authenticate client for resouce owner and assigns resource owner into conn.assigns[:current_user] key. If it fails, then it halts connection and returns :bad_request, :unauthorized or :forbidden status codes with error json

Functions

call(conn, scopes)

Plug function to authenticate client for resouce owner and assigns resource owner into conn.assigns[:current_user] key. If it fails, then it halts connection and returns :bad_request, :unauthorized or :forbidden status codes with error json.

There is one option:

  • scopes - the function used to authorize the resource access
  • the default is “”

Examples

defmodule SomeModule.AppController do
  use SomeModule.Web, :controller
  plug Gandalf.Plug.Authenticate, [scopes: ~w(read write)]

  def index(conn, _params) do
    # access to current user on successful authentication
    current_user = conn.assigns[:current_user]
    ...
  end
end

defmodule SomeModule.AppController do
  use SomeModule.Web, :controller

  plug Gandalf.Plug.Authenticate [scopes: ~w(read write)] when action in [:create]

  def index(conn, _params) do
    # anybody can call this action
    ...
  end

  def create(conn, _params) do
    # only logged in users can access this action
    current_user = conn.assigns[:current_user]
    ...
  end
end
init(opts)