VoileWeb.UserAuth (Voile v0.1.26)

Copy Markdown View Source

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.

Checks if a user needs to complete onboarding. Returns true if the user lacks basic profile information. Only super_admin users are exempt from onboarding.

Handles mounting and authenticating the current_scope in LiveViews.

Plug for routes that require the user to be authenticated.

Plug that ensures the user has completed onboarding before accessing protected routes. This should be used after require_authenticated_user.

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. Also checks if the user is suspended and logs them out if so.

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.

needs_onboarding?(user)

Checks if a user needs to complete onboarding. Returns true if the user lacks basic profile information. Only super_admin users are exempt from onboarding.

on_mount(arg1, 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.

  • {:require_permission, "permission.name"} - Requires both authentication and a specific permission. Checks RBAC permissions.

  • {:require_permission, "permission.name", scope: {:collection, :id}} - Requires authentication and a scoped permission.

  • :require_authenticated_verified_member_organization_or_verified_staff_user - Requires a logged in user who is administrator, staff, or a verified member type (organization or verified individual).

Examples

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

defmodule VoileWeb.PageLive do
  use VoileWeb, :live_view

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

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

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

For permission-based authorization in LiveViews:

live_session :admin_only,
  on_mount: [
    {VoileWeb.UserAuth, :require_authenticated},
    {VoileWeb.UserAuth, {:require_permission, "system.settings"}}
  ] do
  live "/admin", AdminLive, :index
end

require_authenticated_user(conn, opts)

Plug for routes that require the user to be authenticated.

require_onboarding_complete(conn, opts)

Plug that ensures the user has completed onboarding before accessing protected routes. This should be used after require_authenticated_user.

signed_in_path(user)

Returns the path to redirect to after log in.