mix supabase.gen.auth (supabase_gotrue v0.5.2)
View SourceGenerates authentication logic backed by Supabase and related views for Phoenix
.
$ mix supabase.gen.auth MyAppWeb [options]
LiveView vs conventional Controllers & Views
Authentication views can either be generated to use LiveView by passing
the --live
option, or they can use conventional Phoenix
Controllers & Views by passing --no-live
(default).
Using the --live
option is advised if you plan on using LiveView
elsewhere in your application. The user experience when navigating between
LiveViews can be tightly controlled, allowing you to let your users navigate
to authentication views without necessarily triggering a new HTTP request
each time (which would result in a full page load).
Warning
This task relies on Phoenix CoreComponents and Phoenix templates for now, of course you can edit the generated files to fit your needs.
Strategies
The --strategy
(-s
) option can be used to specify the authentication strategy.
The default strategy is password
. Also, multiple strategies can be used in the same application
with multiple -s
options.
The available strategies are:
password
- Email and password authentication.oauth
- OAuth authentication.anon
- Anonymous authentication.id_token
- ID token authentication.sso
- Single sign-on authentication.otp
- One-time password authentication.
For each strategy, a log_in_with_<strategy>
function will be generated in the MyAppWeb.Auth
module, where MyApp
is the name of your application.
Example
$ mix supabase.gen.auth MyAppWeb --strategy password --strategy oauth
Options
--live
- Generate LiveView authentication views.--no-live
- Generate conventional Phoenix Controllers & Views.--strategy
- The authentication strategy to use. Defaults topassword
.--client
- The Supabase self managed client to use for authentication.--supabase-url
- The Supabase URL in case to use one-off client. Check the configuration section below.--supabase-key
- The Supabase API key in case to use one-off client. Check the configuration section below.--auth-only
- Generate only the authentication module without views, controllers or routes.
Configuration
To use this task, you need to have at least supabase_potion
and supabase_gotrue
packages installed in your project, and phoenix_live_view
if you want to use LiveView or phoenix
and phoenix_plug
if you want to use conventional Controllers & (dead) Views.
Also, you need to tell the task which Supabase
client to be used in the generated functions,
for that you have two available options following the supabase-ex docs
Self-managed client
The best way is to define a self-managed Supabase
client in your config.exs
and app. You can follow the documentation about it.
# lib/my_app/supabase.ex
defmodule MyApp.Supabase do
use Supabase.Client, otp_app: :my_app
end
# config/config.exs
import Config
config :my_app, MyApp.Supabase,
base_url: "https://<app-name>.supabase.co",
api_key: "<supabase-api-key>",
# any additional optional params
access_token: "<supabase-access-token>",
db: [schema: "another"],
auth: [debug: true] # optional
Then you can invoke this task passing the basic options in addition to the --client
option:
$ mix supabase.gen.auth MyAppWeb -s anon --client MyApp.Supabase
This is the best option if you are going to use the same client in other parts of your application and also to hold and handle authentication tokens for different scopes via Supabase.Client.update_access_token/2
.
One-off client
If you don't want to define a self-managed client, you can pass the --supabase-url
and --supabase-key
options to the task:
$ mix supabase.gen.auth MyAppWeb -s anon --supabase-url https://<app-name>.supabase.co --supabase-key <supabase-api-key>
This option is useful if you are going to use the client only in the authentication logic and don't need to handle tokens for different scopes.
Generated Files
LiveView
lib/my_app_web/router.ex
- The authentication routes, modifies the existing one in-place.lib/my_app_web/user_auth.ex
- The authentication module.lib/my_app_web/controllers/session_controller.ex
- The session controller, with token handling.lib/my_app_web/live/login_live.ex
- The LiveView for the login page.lib/my_app_web/live/registration_live.ex
- The LiveView for the registration page.test/support/conn_case.exs
- The test helper for the authentication, modifies the existing one in-place.
Non-LiveView (Traditional Phoenix Controllers & Views)
lib/my_app_web/router.ex
- The authentication routes, modifies the existing one in-place.lib/my_app_web/user_auth.ex
- The authentication module.lib/my_app_web/controllers/session_controller.ex
- The session controller, with token handling.lib/my_app_web/controllers/session_html.ex
- The session view, with the login form.lib/my_app_web/controllers/session_html/new.html.heex
- The login form.lib/my_app_web/controllers/registration_controller.ex
- The registration controller.lib/my_app_web/controllers/registration_html.ex
- The registration view.lib/my_app_web/controllers/registration_html/new.html.heex
- The registration form.test/support/conn_case.exs
- The test helper for the authentication, modifies the existing one in-place.