authsense v1.0.0 Authsense.Plug

Plug helpers for Authsense.

You can use most of these functions as plugs.

# in your controller or pipeline:
import Authsense.Plug
plug :fetch_current_user

You can specify additional options.

plug :fetch_current_user,
  repo: MyApp.Repo,
  model: MyApp.User

Summary

Functions

Sets the :current_user assigns variable based on session

Sets the current user for the session

Functions

fetch_current_user(conn, opts \\ [])

Sets the :current_user assigns variable based on session.

# in your controller or pipeline:
import Authsense.Plug
plug :fetch_current_user

By doing so, you’ll get access to the :current_user assigns. It will be set to the User model if logged in, or to nil if logged out.

conn.assigns.current_user

<%= if @current_user %>
  Hello, <%= @current_user.name %>
<% else %>
  You are not logged in.
<% end %>
put_current_user(conn, user)

Sets the current user for the session.

import Authsense.Plug

conn
|> put_current_user(user)
|> put_flash(:info, "Welcome.")
|> redirect(to: "/")

To logout, set it to nil.

conn
|> put_current_user(nil)
|> put_flash(:info, "You've been logged out.")
|> redirect(to: "/")

This sets the :current_user_id in the Session store.