JumarWeb.UserAuth (Jumar v0.1.0)

View Source

Handles user authentication, session management, and "remember me" functionality.

Summary

Functions

Disconnects existing sockets for the given tokens.

Authenticates the user by looking into the session and remember me token.

Logs the user out.

Handles mounting and authenticating the current_scope in LiveViews.

Plug for routes that require the user to be authenticated.

Returns the path to redirect to after log in.

Functions

disconnect_sessions(tokens)

Disconnects existing sockets for the given tokens.

fetch_current_scope_for_user(conn, opts)

Authenticates the user by looking into the session and remember me token.

Will reissue the session token if it is older than the configured age.

log_in_user(conn, user, params \\ %{})

Logs the user in.

Redirects to the session's :user_return_to path or falls back to the signed_in_path/1.

log_out_user(conn)

Logs the user out.

It clears all session data for safety. See renew_session.

on_mount(atom, params, session, socket)

Handles mounting and authenticating the current_scope in LiveViews.

on_mount arguments

  • :mount_current_scope - Assigns current_scope to socket assigns based on user_token, or nil if there's no user_token or no matching user.

  • :require_authenticated - Authenticates the user from the session, and assigns the current_scope to socket assigns based on user_token. Redirects to login page if there's no logged user.

Examples

Use the on_mount lifecycle macro in LiveViews to mount or authenticate the current_scope:

defmodule JumarWeb.PageLive do
  use JumarWeb, :live_view

  on_mount {JumarWeb.UserAuth, :mount_current_scope}
  ...
end

Or use the live_session of your router to invoke the on_mount callback:

live_session :authenticated, on_mount: [{JumarWeb.UserAuth, :require_authenticated}] do
  live "/profile", ProfileLive, :index
end

require_authenticated_user(conn, opts)

Plug for routes that require the user to be authenticated.

signed_in_path(arg1)

Returns the path to redirect to after log in.