Supabase.GoTrue.Plug (supabase_gotrue v0.3.7)
Provides Plug-based authentication support for the Supabase GoTrue authentication in Elixir applications.
This module offers a series of functions to manage user authentication through HTTP requests in Phoenix applications. It facilitates operations like logging in with a password, logging out users, fetching the current user from a session, and handling route protections based on authentication state.
Configuration
The module requires some application environment variables to be set:
authentication_client
: The Supabase client used for authentication.signed_in_path
: The route to where conn should be redirected to after authenticationnot_authenticated_path
: The route to where conn should be redirect to if user isn't authenticated
You can set up these config in your config.exs
:
config :supabase_gotrue,
signed_in_path: "/dashboard",
not_authenticated_path: "/login",
authentication_client: :my_supabase_potion_client_name
Authentication Flow
It handles session management, cookie operations, and redirects based on user authentication status, providing a seamless integration for user sessions within Phoenix routes.
Summary
Functions
Retrieves the current user from the session or a signed cookie, assigning it to the connection's assigns.
Logs in a user using a username and password. Stores the user token in the session and a cookie, if a "remember_me"
key is present inside params
.
Logs out the user from the application, clearing session data
Redirects an user to the configured signed_in_path
if it is authenticated, if not, just halts the connection.
Ensures an user is authenticated before executing the rest of Plugs chain.
Functions
fetch_current_user(conn, opts)
Retrieves the current user from the session or a signed cookie, assigning it to the connection's assigns.
Can be easily used as a plug, for example inside a Phoenix web app
pipeline in your YourAppWeb.Router
, you can do something like:
import Supabase.GoTrue.Plug
pipeline :browser do
plug :fetch_session # comes from Plug.Conn
plug :fetch_current_user
# rest of plug chain...
end
log_in_with_id_token(conn, params \\ %{})
log_in_with_oauth(conn, params \\ %{})
log_in_with_otp(conn, params \\ %{})
log_in_with_password(conn, params \\ %{})
Logs in a user using a username and password. Stores the user token in the session and a cookie, if a "remember_me"
key is present inside params
.
For more information on how Supabase login with email and password works, check Supabase.GoTrue.sign_in_with_password/2
log_in_with_sso(conn, params \\ %{})
log_out_user(conn, scope)
Logs out the user from the application, clearing session data
put_token_in_session(conn, token)
redirect_if_user_is_authenticated(conn, opts)
Redirects an user to the configured signed_in_path
if it is authenticated, if not, just halts the connection.
Generaly you wan to use it inside your scopes routes inside YourAppWeb.Router
:
scope "/" do
pipe_trough [:browser, :redirect_if_user_is_authenticated]
get "/login", LoginController, :login
end
require_authenticated_user(conn, opts)
Ensures an user is authenticated before executing the rest of Plugs chain.
Generaly you wan to use it inside your scopes routes inside YourAppWeb.Router
:
scope "/" do
pipe_trough [:browser, :require_authenticated_user]
get "/super-secret", SuperSecretController, :secret
end