Skip to content

supabase-community/auth-ex

Repository files navigation

Supabase GoTrue

hex.pm docs ci

Auth implementation for the Supabase Potion SDK in Elixir.

Installation

def deps do
  [
    {:supabase_potion, "~> 0.6"},
    {:supabase_gotrue, "~> 0.4"}
  ]
end

Quick Start

  1. 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
  1. Create your Supabase client:
defmodule MyApp.Supabase.Client do
  use Supabase.Client, otp_app: :my_app
end
  1. 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)

Documentation

Authentication Methods

  • 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

Integration Options

Traditional Web Applications (Plug/Phoenix)

# 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

Phoenix LiveView Applications

# 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

Examples

Check the Supabase Potion examples showcase for sample applications.