Skip to content

Commit 45a47ad

Browse files
committed
Confirm user when signing in via magic link
1 parent 53560c9 commit 45a47ad

File tree

4 files changed

+55
-14
lines changed

4 files changed

+55
-14
lines changed

app/lib/warden_extensions/setup.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,20 @@ def configure_manager
1414
Warden::Manager.after_authentication do |user, auth, opts|
1515
case opts[:scope]
1616
when :user
17-
user.touch(:last_sign_in_at)
17+
user.signed_in!
18+
when :admin_user
19+
# no op
1820
end
19-
end
2021

21-
# Hooks
22-
# Warden::Manager.after_set_user do |user, auth, opts|
23-
# unless user.active?
24-
# auth.logout
25-
# throw(:warden, :message => "User not active")
26-
# end
27-
# end
22+
case auth.winning_strategy&.key
23+
when :magic_session
24+
user.confirm!
25+
when :password
26+
# no op
27+
else # nil, as with test helpers
28+
# no op
29+
end
30+
end
2831
end
2932
end
3033
end

app/models/user.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ def confirm!
4242
end
4343
end
4444

45-
update_column(:confirmed_at, Time.current)
45+
touch :confirmed_at
46+
end
47+
48+
def signed_in!
49+
touch :last_sign_in_at
4650
end
4751

4852
def confirmed?

spec/requests/users/magic_session_tokens_spec.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,24 @@
2323
end
2424
end
2525

26-
describe "POST create" do
26+
describe "GET show" do
2727
it "succeeds" do
28+
get new_users_magic_session_token_path
29+
30+
expect(response).to have_http_status(:ok)
31+
end
32+
33+
it "redirects if authenticated" do
34+
login_user
35+
36+
get new_users_magic_session_token_path
37+
38+
expect(response).to have_http_status(:found)
39+
end
40+
end
41+
42+
describe "POST create" do
43+
it "succeeds for confirmed user" do
2844
user = FactoryBot.create(:user, :confirmed)
2945
post users_magic_session_tokens_path, params: {user: {email: user.email}}
3046

spec/requests/users/sessions_spec.rb

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
end
2323

2424
describe "POST create" do
25-
it "signs in user with valid email and password" do
25+
it "signs in confirmed user with valid email and password" do
2626
user = FactoryBot.create(:user, :confirmed, password: "password", password_confirmation: "password")
2727
expect(user.last_sign_in_at).to be_nil
2828

@@ -35,7 +35,7 @@
3535
expect(user.reload.last_sign_in_at).to be_present
3636
end
3737

38-
it "signs in user with valid magic session token" do
38+
it "signs in confirmed user with valid magic session token" do
3939
user = FactoryBot.create(:user, :confirmed)
4040
token = user.generate_token_for(:magic_session)
4141
expect(user.last_sign_in_at).to be_nil
@@ -49,14 +49,32 @@
4949
expect(user.reload.last_sign_in_at).to be_present
5050
end
5151

52+
it "signs in unconfirmed user with valid magic session token" do
53+
user = FactoryBot.create(:user, :unconfirmed)
54+
token = user.generate_token_for(:magic_session)
55+
expect(user.last_sign_in_at).to be_nil
56+
57+
post users_sessions_path, params: {token: token}
58+
59+
perform_enqueued_jobs_and_subsequently_enqueued_jobs
60+
61+
expect(response).to redirect_to(users_dashboard_path)
62+
expect(flash[:notice]).to eq("Signed in successfully")
63+
64+
user.reload
65+
66+
expect(user.last_sign_in_at).to be_present
67+
expect(user).to be_confirmed
68+
end
69+
5270
it "disallows when user not found with given email" do
5371
post users_sessions_path, params: {user: {email: "hello#{SecureRandom.hex(5)}@example.com", password: "password"}}
5472

5573
expect(response).to have_http_status(:unprocessable_entity)
5674
expect(flash[:alert]).to eq("Incorrect email or password")
5775
end
5876

59-
it "disallows when user is not confirmed" do
77+
it "disallows password sign in when user is not confirmed" do
6078
user = FactoryBot.create(:user, :unconfirmed, password: "password", password_confirmation: "password")
6179
post users_sessions_path, params: {user: {email: user.email, password: "password"}}
6280

0 commit comments

Comments
 (0)