Skip to content

Commit 5dbbcaa

Browse files
committed
Cleanup & simplify sign in
1 parent b74d8b0 commit 5dbbcaa

File tree

5 files changed

+15
-13
lines changed

5 files changed

+15
-13
lines changed

app/controllers/concerns/authentication.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,9 @@ def ensure_development_magic_link_not_leaked
108108
end
109109

110110
def serve_development_magic_link(magic_link)
111-
if Rails.env.development?
112-
flash[:magic_link_code] = magic_link&.code
111+
if Rails.env.development? && magic_link.present?
112+
flash[:magic_link_code] = magic_link.code
113+
response.set_header("X-Magic-Link-Code", magic_link.code)
113114
end
114115
end
115116
end

app/controllers/identities_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class IdentitiesController < ApplicationController
22
disallow_account_scope
3-
3+
44
def show
55
@identity = Current.identity
66
end

app/controllers/sessions/magic_links_controller.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ def respond_to_valid_code_from(magic_link)
3030

3131
format.json do
3232
new_access_token = magic_link.identity.access_tokens.create!(permission: :write)
33-
render json: {
33+
render json: {
3434
email_address: magic_link.identity.email_address,
3535
access_token: new_access_token.token,
3636
users: magic_link.identity.users
37-
}
37+
}
3838
end
3939
end
4040
end
@@ -55,7 +55,8 @@ def after_sign_in_url(magic_link)
5555
end
5656

5757
def rate_limit_exceeded
58-
rate_limit_exceeded_message = "Try again later."
58+
rate_limit_exceeded_message = "Try again in 15 minutes."
59+
5960
respond_to do |format|
6061
format.html { redirect_to session_magic_link_path, alert: rate_limit_exceeded_message }
6162
format.json { render json: { message: rate_limit_exceeded_message }, status: :too_many_requests }

app/controllers/sessions_controller.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,17 @@ def new
99
end
1010

1111
def create
12-
13-
identity = Identity.find_by_email_address(email_address)
14-
magic_link = identity&.send_magic_link
12+
if identity = Identity.find_by_email_address(email_address)
13+
magic_link = identity.send_magic_link
14+
serve_development_magic_link(magic_link)
15+
end
1516

1617
respond_to do |format|
17-
format.html do
18-
serve_development_magic_link(magic_link)
18+
format.html do
1919
redirect_to session_magic_link_path
2020
end
2121

2222
format.json do
23-
response.set_header("X-Magic-Link-Code", magic_link&.code) if Rails.env.development? && magic_link
2423
head :created
2524
end
2625
end
@@ -38,6 +37,7 @@ def email_address
3837

3938
def rate_limit_exceeded
4039
rate_limit_exceeded_message = "Try again later."
40+
4141
respond_to do |format|
4242
format.html { redirect_to new_session_path, alert: rate_limit_exceeded_message }
4343
format.json { render json: { message: rate_limit_exceeded_message }, status: :too_many_requests }

app/views/identities/show.json.jbuilder

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ json.accounts @identity.users do |user|
22
json.partial! "identities/account", account: user.account
33
json.user do
44
json.partial! "users/user", user: user
5-
end
5+
end
66
end

0 commit comments

Comments
 (0)