Class: Supabase::Rails::OtpController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/supabase/rails/otp_controller.rb

Overview

Passwordless sign-in via OTP / magic link (FR-W7 / US-013):

* `new` / `create` — user supplies an email or phone; gem calls
  {Supabase::Rails::Authentication#supabase_sign_in_with_otp} which
  triggers delivery (no session yet). Routes to `verify`.
* `verify` — accepts both GET (render the code-entry form) and POST
  (submit the code via
  {Supabase::Rails::Authentication#supabase_verify_otp}). On success
  the helper writes the session cookie and we redirect to
  `after_authentication_url`.

Instance Method Summary collapse

Methods included from Authentication

#after_authentication_url, #authenticate_with_supabase, #authenticated?, #current_user, expose_current_user?, railtie_config, redact_email, #request_authentication, #require_authentication, #start_new_session_for, #store_location_for_redirect, #stored_location_for_redirect, #supabase_exchange_code_for_session, #supabase_resend, #supabase_reset_password, #supabase_sign_in_with_oauth, #supabase_sign_in_with_otp, #supabase_sign_in_with_password, #supabase_sign_up, #supabase_update_user, #supabase_verify_otp, #terminate_session

Instance Method Details

#createObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/supabase/rails/otp_controller.rb', line 20

def create
  result = (
    email: params[:email],
    phone: params[:phone]
  )

  if result.success?
    redirect_to verify_otp_index_path,
                notice: I18n.t("supabase.rails.otp.sent")
  else
    flash.now[:alert] = result.error.message
    render :new, status: :unprocessable_entity
  end
end

#newObject



18
# File 'app/controllers/supabase/rails/otp_controller.rb', line 18

def new; end

#verifyObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/supabase/rails/otp_controller.rb', line 35

def verify
  return unless request.post?

  result = supabase_verify_otp(
    token: params[:token],
    type: params[:type] || "email",
    email: params[:email],
    phone: params[:phone]
  )

  if result.success?
    redirect_to after_authentication_url,
                notice: I18n.t("supabase.rails.otp.verified")
  else
    flash.now[:alert] = result.error.message
    render :verify, status: :unprocessable_entity
  end
end