Add JSON format support to signup flow for native API clients
The magic link verification endpoint now returns `requires_signup_completion` to indicate whether the client needs to collect a name and complete signup. The signup completion endpoint now supports JSON responses with the account ID. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
committed by
Rosa Gutierrez
parent
231fa311c1
commit
614527e9c2
@@ -46,7 +46,7 @@ class Sessions::MagicLinksController < ApplicationController
|
||||
|
||||
respond_to do |format|
|
||||
format.html { redirect_to after_sign_in_url(magic_link) }
|
||||
format.json { render json: { session_token: session_token } }
|
||||
format.json { render json: { session_token: session_token, requires_signup_completion: requires_signup_completion?(magic_link) } }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -68,7 +68,7 @@ class Sessions::MagicLinksController < ApplicationController
|
||||
end
|
||||
|
||||
def after_sign_in_url(magic_link)
|
||||
if magic_link.for_sign_up?
|
||||
if requires_signup_completion?(magic_link)
|
||||
new_signup_completion_path
|
||||
else
|
||||
after_authentication_url
|
||||
@@ -82,4 +82,8 @@ class Sessions::MagicLinksController < ApplicationController
|
||||
format.json { render json: { message: rate_limit_exceeded_message }, status: :too_many_requests }
|
||||
end
|
||||
end
|
||||
|
||||
def requires_signup_completion?(magic_link)
|
||||
magic_link.for_sign_up?
|
||||
end
|
||||
end
|
||||
|
||||
@@ -11,10 +11,9 @@ class Signups::CompletionsController < ApplicationController
|
||||
@signup = Signup.new(signup_params)
|
||||
|
||||
if @signup.complete
|
||||
flash[:welcome_letter] = true
|
||||
redirect_to landing_url(script_name: @signup.account.slug)
|
||||
welcome_to_account
|
||||
else
|
||||
render :new, status: :unprocessable_entity
|
||||
invalid_signup
|
||||
end
|
||||
end
|
||||
|
||||
@@ -22,4 +21,22 @@ class Signups::CompletionsController < ApplicationController
|
||||
def signup_params
|
||||
params.expect(signup: %i[ full_name ]).with_defaults(identity: Current.identity)
|
||||
end
|
||||
|
||||
def welcome_to_account
|
||||
respond_to do |format|
|
||||
format.html do
|
||||
flash[:welcome_letter] = true
|
||||
redirect_to landing_url(script_name: @signup.account.slug)
|
||||
end
|
||||
|
||||
format.json { render json: { account_id: @signup.account.external_account_id }, status: :created }
|
||||
end
|
||||
end
|
||||
|
||||
def invalid_signup
|
||||
respond_to do |format|
|
||||
format.html { render :new, status: :unprocessable_entity }
|
||||
format.json { render json: { errors: @signup.errors.full_messages }, status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -80,7 +80,7 @@ class Sessions::MagicLinksControllerTest < ActionDispatch::IntegrationTest
|
||||
assert MagicLink.exists?(expired_link.id), "Expired magic link should not be consumed"
|
||||
end
|
||||
|
||||
test "create via JSON" do
|
||||
test "create via JSON for sign in" do
|
||||
identity = identities(:david)
|
||||
magic_link = identity.send_magic_link
|
||||
|
||||
@@ -89,6 +89,20 @@ class Sessions::MagicLinksControllerTest < ActionDispatch::IntegrationTest
|
||||
post session_magic_link_path(format: :json), params: { code: magic_link.code }
|
||||
assert_response :success
|
||||
assert @response.parsed_body["session_token"].present?
|
||||
assert_equal false, @response.parsed_body["requires_signup_completion"]
|
||||
end
|
||||
end
|
||||
|
||||
test "create via JSON for sign up" do
|
||||
identity = identities(:david)
|
||||
magic_link = identity.send_magic_link(for: :sign_up)
|
||||
|
||||
untenanted do
|
||||
post session_path(format: :json), params: { email_address: identity.email_address }
|
||||
post session_magic_link_path(format: :json), params: { code: magic_link.code }
|
||||
assert_response :success
|
||||
assert @response.parsed_body["session_token"].present?
|
||||
assert_equal true, @response.parsed_body["requires_signup_completion"]
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -55,4 +55,30 @@ class Signup::CompletionsControllerTest < ActionDispatch::IntegrationTest
|
||||
assert_select "li", text: "Full name can't be blank"
|
||||
end
|
||||
end
|
||||
|
||||
test "create via JSON" do
|
||||
untenanted do
|
||||
post signup_completion_path(format: :json), params: {
|
||||
signup: {
|
||||
full_name: @signup.full_name
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
assert_response :created
|
||||
assert @response.parsed_body["account_id"].present?
|
||||
end
|
||||
|
||||
test "create via JSON with blank name" do
|
||||
untenanted do
|
||||
post signup_completion_path(format: :json), params: {
|
||||
signup: {
|
||||
full_name: ""
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
assert_response :unprocessable_entity
|
||||
assert_includes @response.parsed_body["errors"], "Full name can't be blank"
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user