mix supabase. gen. auth
(supabase_auth v1.0.1)
View Source
Generates 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.--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_auth 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.
The generated authentication functions expect a Supabase.Client to be passed explicitly as a parameter. This gives you full control over how you manage and provide the client.
Setting up your Supabase Client
Create a client using Supabase.init_client!/2:
client = Supabase.init_client!(
System.get_env("SUPABASE_URL") || raise("SUPABASE_URL not set"),
System.get_env("SUPABASE_KEY") || raise("SUPABASE_KEY not set")
)How you store and provide the client is up to you (e.g. in a plug, application config, or a helper module).
Then you can invoke this task with just the basic options:
$ mix supabase.gen.auth MyAppWeb -s password -s oauth
Using the generated auth functions
All generated functions accept a %Supabase.Client{} as a parameter. You provide the client when calling these functions:
# In a controller
def create(conn, %{"user" => user_params}) do
client = Supabase.init_client!("https://myapp.supabase.co", "your-anon-key")
MyAppWeb.UserAuth.log_in_user_with_password(conn, client, user_params)
end
# In a LiveView
def mount(_params, _session, socket) do
client = Supabase.init_client!("https://myapp.supabase.co", "your-anon-key")
socket = MyAppWeb.UserAuth.assign_supabase_client(socket, client)
{:ok, socket}
endGenerated 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.