Module: Supabase::Rails::Routes

Defined in:
lib/supabase/rails/routes.rb

Overview

‘supabase_authentication_routes` routing-DSL helper (FR-W12 / US-022).

Installed onto ‘ActionDispatch::Routing::Mapper` by Engine so hosts can write a single line inside `Rails.application.routes.draw do … end` to mount the entire auth surface that the gem’s controllers (US-017) and views (US-020) expect.

Expansion (default — no filter):

* `resource :session, only: %i[new create destroy]`
* `resource :registration, only: %i[new create]`
* `resources :passwords, only: %i[new create edit update], param: :token`
* `resources :otp, only: %i[new create]` with a `verify` collection
  route responding to both GET (render the code-entry form) and POST
  (submit the code). Named `verify_otp_index_path`.
* `GET /oauth/:provider/authorize` → `oauth_authorize_path(provider)`
* `GET /oauth/callback`            → `oauth_callback_path`

‘only:` / `except:` filter by group symbol — one of `:session`, `:registration`, `:passwords`, `:otp`, `:oauth` (per OQ-W4 lean-yes). A host that only wants password resets can write:

supabase_authentication_routes only: %i[passwords]

The helper does NOT name controllers — by default ‘resource :session` routes to `::SessionsController`, which is exactly the top-level wrapper the install generator (US-018) creates. Hosts that nest auth under a scope can wrap the call:

scope module: "auth" do
  supabase_authentication_routes
end

Constant Summary collapse

GROUPS =
%i[session registration passwords otp oauth].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.filter(only: nil, except: nil) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/supabase/rails/routes.rb', line 69

def self.filter(only: nil, except: nil)
  validate_filter!(:only,   only)   if only
  validate_filter!(:except, except) if except

  groups = GROUPS.dup
  groups &= Array(only).map(&:to_sym)   if only
  groups -= Array(except).map(&:to_sym) if except
  groups
end

.validate_filter!(name, value) ⇒ Object

Raises:

  • (ArgumentError)


79
80
81
82
83
84
85
86
# File 'lib/supabase/rails/routes.rb', line 79

def self.validate_filter!(name, value)
  unknown = Array(value).map(&:to_sym) - GROUPS
  return if unknown.empty?

  raise ArgumentError,
        "supabase_authentication_routes: unknown #{name}: #{unknown.inspect}. " \
        "Valid groups: #{GROUPS.inspect}"
end

Instance Method Details

#supabase_authentication_routes(only: nil, except: nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/supabase/rails/routes.rb', line 40

def supabase_authentication_routes(only: nil, except: nil)
  groups = Routes.filter(only: only, except: except)

  if groups.include?(:session)
    resource :session, only: %i[new create destroy]
  end

  if groups.include?(:registration)
    resource :registration, only: %i[new create]
  end

  if groups.include?(:passwords)
    resources :passwords, only: %i[new create edit update], param: :token
  end

  if groups.include?(:otp)
    resources :otp, only: %i[new create] do
      collection do
        match :verify, via: %i[get post], as: :verify
      end
    end
  end

  if groups.include?(:oauth)
    get "/oauth/:provider/authorize", to: "oauth#authorize", as: :oauth_authorize
    get "/oauth/callback",            to: "oauth#callback",  as: :oauth_callback
  end
end