Supabase.GoTrue.LiveView (supabase_gotrue v0.3.7)

Provides LiveView integrations for the Supabase GoTrue authentication in Elixir applications.

This module enables the seamless integration of authentication flows within Phoenix LiveView applications by leveraging the Supabase GoTrue SDK. It supports operations such as mounting current users, handling authenticated and unauthenticated states, and logging out users.

Configuration

The module requires some application environment variables to be set:

  • authentication_client: The Supabase client used for authentication.
  • endpoint: Your web app endpoint, used internally for broadcasting user disconnection events.
  • signed_in_path: The route to where socket should be redirected to after authentication

You can set up these config in your config.exs:

config :supabase_gotrue,
  endpoint: YourApp.Endpoint,
  signed_in_path: "/dashboard",
  authentication_client: :my_supabase_potion_client_name

Usage

Typically, this module is used in your YourAppWeb.Router, to handle user authentication states through a series of on_mount callbacks, which ensure that user authentication logic is processed during the LiveView lifecycle.

Check on_mount/4 for more detailed usage instructions on LiveViews

Summary

Functions

Logs out the user from the session and broadcasts a disconnect event.

Handles mounting and authenticating the current_user in LiveViews.

Functions

Link to this function

log_out_user(socket, scope)

Logs out the user from the session and broadcasts a disconnect event.

Parameters

Examples

iex> log_out_user(socket, :local)
# Broadcasts 'disconnect' and removes the user session
Link to this function

mount_current_user(session, socket)

Link to this function

on_mount(atom, params, session, socket)

Handles mounting and authenticating the current_user in LiveViews.

on_mount arguments

  • :mount_current_user - Assigns current_user to socket assigns based on user_token, or nil if there's no user_token or no matching user.
  • :ensure_authenticated - Authenticates the user from the session, and assigns the current_user to socket assigns based on user_token. Redirects to login page if there's no logged user.
  • :redirect_if_user_is_authenticated - Authenticates the user from the session. Redirects to signed_in_path if there's a logged user.

Examples

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

defmodule PescarteWeb.PageLive do
  use PescarteWeb, :live_view
  on_mount {PescarteWeb.Authentication, :mount_current_user}
  ...
end

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

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