LightningWeb.UserAuth (Lightning v2.19.0-pre)

View Source

The UserAuth controller.

Summary

Functions

Tears down both transports for every live WebSocket the user has open, on all devices.

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

The /live transport topic addressing every LiveView connection the user has open.

Logs the user in by creating a new session token.

Logs the user out.

The flash message shown when login is refused for a given Lightning.Accounts.login_blocked_reason/1 value. Shared by the password and SSO login paths so their wording can't drift apart.

Assigns the token to a new session.

Used for LiveView routes that require the user to be reauthenticated.

Reauthenticate the user by using the sudo token

Used for routes that require the user to not be authenticated.

Returns to or redirects to the dashboard and potentially set remember_me token.

Used for API routes that require the resource to be authenticated. A resource can be a User or a ProjectRepoConnection

Used for routes that require the user to be authenticated.

Used for routes that require the user to be reauthenticated.

Require that the user has the superuser role

The /socket transport topic addressing every LightningWeb.UserSocket connection the user has open.

Functions

authenticate_bearer(conn, opts)

disconnect_user_sockets(user)

Tears down both transports for every live WebSocket the user has open, on all devices.

Call this after an account-wide revocation (password change or reset, account disable) has invalidated the user's sessions, so both drop immediately instead of staying authorised until they happen to reconnect.

Logout does not use this; see log_out_user/1.

fetch_current_user(conn, opts)

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

This is where :live_socket_id is written, so a revocation can reach the pages the user already has open.

get_bearer(conn)

live_socket_topic(user)

The /live transport topic addressing every LiveView connection the user has open.

Keyed per user rather than per session token, so an authorisation change reaches pages that are already open. phx.gen.auth keys it per token; the LiveView security-model guide keys it per user, and we follow the guide.

We don't use the guide's name for it, users_socket:. That is one character from user_socket: above, and both are broadcast from the same function.

log_in_user(conn, user)

Logs the user in by creating a new session token.

log_out_user(conn)

Logs the user out.

It clears all session data for safety. See renew_session.

This hangs up the account's LiveView connections on every device, but revokes only the token it was logged out of, so the other devices reconnect and stay signed in.

login_blocked_message(atom)

@spec login_blocked_message(:disabled | :scheduled_deletion) :: String.t()

The flash message shown when login is refused for a given Lightning.Accounts.login_blocked_reason/1 value. Shared by the password and SSO login paths so their wording can't drift apart.

Only the two blocked reasons are valid inputs; gate with Lightning.Accounts.login_blocked?/1.

mark_totp_pending(conn)

new_session(conn, token)

Assigns the token to a new session.

It renews the session ID and clears the whole session to avoid fixation attacks. See the renew_session function to customize this behaviour.

on_mount(atom, params, session, socket)

Used for LiveView routes that require the user to be reauthenticated.

reauth_sudo_mode(conn, opts)

Reauthenticate the user by using the sudo token

redirect_if_user_is_authenticated(conn, opts)

Used for routes that require the user to not be authenticated.

redirect_with_return_to(conn, params \\ %{})

Returns to or redirects to the dashboard and potentially set remember_me token.

require_authenticated_api_resource(conn, opts)

Used for API routes that require the resource to be authenticated. A resource can be a User or a ProjectRepoConnection

require_authenticated_user(conn, opts)

Used for routes that require the user to be authenticated.

Refuses a session whose second factor is still pending, and one whose email address is unconfirmed past the deadline — the latter is confined to the handful of paths in LightningWeb.ConfirmationLockout.

require_sudo_user(conn, opts)

Used for routes that require the user to be reauthenticated.

require_superuser(conn, opts)

Require that the user has the superuser role

totp_pending?(conn)

totp_validated(conn)

user_socket_topic(user)

The /socket transport topic addressing every LightningWeb.UserSocket connection the user has open.