Auth implementation for the Supabase Potion SDK in Elixir.
def deps do
[
{:supabase_potion, "~> 0.6"},
{:supabase_gotrue, "~> 0.4"}
]
end
- Configure your Supabase client in
config.exs
:
import Config
config :my_app, MyApp.Supabase.Client,
base_url: "https://myapp.supabase.co",
api_key: "myapp-api-key"
config :supabase_gotrue, auth_module: MyAppWeb.Auth
- Create your Supabase client:
defmodule MyApp.Supabase.Client do
use Supabase.Client, otp_app: :my_app
end
- Use the authentication functions:
# Sign in with email and password
{:ok, session} = Supabase.GoTrue.sign_in_with_password(client, %{
email: "[email protected]",
password: "secure-password"
})
# Get the current user
{:ok, user} = Supabase.GoTrue.get_user(client, session)
- HexDocs - Complete API reference
- Authentication Guide - Authentication methods and integration
- MFA Guide - Multi-Factor Authentication
- Sign in with email/password
- Sign in with phone/password
- Sign in with magic link (OTP)
- OAuth (social) authentication
- Single Sign-On (SSO)
- Anonymous sign in
- Multi-factor authentication
# Define your auth module
defmodule MyAppWeb.Auth do
use Supabase.GoTrue.Plug,
client: MyApp.Supabase.Client,
endpoint: MyAppWeb.Endpoint,
signed_in_path: "/app",
not_authenticated_path: "/login"
end
# In your router
defmodule MyAppWeb.Router do
import MyAppWeb.Auth
pipeline :browser do
plug :fetch_current_user
end
# Public routes
scope "/", MyAppWeb do
pipe_through [:browser, :redirect_if_user_is_authenticated]
get "/login", SessionController, :new
post "/login", SessionController, :create
end
# Protected routes
scope "/app", MyAppWeb do
pipe_through [:browser, :require_authenticated_user]
get "/", DashboardController, :index
end
end
# Define your auth module
defmodule MyAppWeb.Auth do
use Supabase.GoTrue.LiveView,
client: MyApp.Supabase.Client,
endpoint: MyAppWeb.Endpoint,
signed_in_path: "/app",
not_authenticated_path: "/login"
end
# In your LiveView
defmodule MyAppWeb.DashboardLive do
use MyAppWeb, :live_view
on_mount {MyAppWeb.Auth, :mount_current_user}
on_mount {MyAppWeb.Auth, :ensure_authenticated}
def mount(_params, _session, socket) do
# socket.assigns.current_user is available here
{:ok, assign(socket, page_title: "Dashboard")}
end
end
# In your router
live_session :authenticated,
on_mount: [
{MyAppWeb.Auth, :mount_current_user},
{MyAppWeb.Auth, :ensure_authenticated}
] do
live "/dashboard", DashboardLive
end
Check the Supabase Potion examples showcase for sample applications.