Skip to content

Commit a689a06

Browse files
bettystegerMaicolBen
authored andcommitted
Resend confirmation instructions (#1267)
* resend confirmation instructions * refactor resend confirmation instructions * fix find_by for mongoid
1 parent 76beb08 commit a689a06

2 files changed

Lines changed: 66 additions & 3 deletions

File tree

app/controllers/devise_token_auth/confirmations_controller.rb

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
module DeviseTokenAuth
44
class ConfirmationsController < DeviseTokenAuth::ApplicationController
5+
56
def show
67
@resource = resource_class.confirm_by_token(params[:confirmation_token])
78

@@ -10,9 +11,6 @@ def show
1011

1112
redirect_header_options = { account_confirmation_success: true }
1213

13-
# give redirect value from params priority or fall back to default value if provided
14-
redirect_url = params[:redirect_url] || DeviseTokenAuth.default_confirm_success_url
15-
1614
if signed_in?(resource_name)
1715
client_id, token = signed_in_resource.create_token
1816

@@ -30,5 +28,31 @@ def show
3028
raise ActionController::RoutingError, 'Not Found'
3129
end
3230
end
31+
32+
def create
33+
return head :bad_request if params[:email].blank?
34+
35+
@resource = resource_class.dta_find_by(uid: params[:email].downcase, provider: provider)
36+
37+
return head :not_found unless @resource
38+
39+
@resource.send_confirmation_instructions({
40+
redirect_url: redirect_url,
41+
client_config: params[:config_name]
42+
})
43+
44+
head :ok
45+
end
46+
47+
private
48+
49+
# give redirect value from params priority or fall back to default value if provided
50+
def redirect_url
51+
params.fetch(
52+
:redirect_url,
53+
DeviseTokenAuth.default_confirm_success_url
54+
)
55+
end
56+
3357
end
3458
end

test/controllers/devise_token_auth/confirmations_controller_test.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,33 @@ def token_and_client_config_from(body)
8686
assert response.body.include?('account_confirmation_success')
8787
end
8888
end
89+
90+
describe 'resend confirmation' do
91+
before do
92+
post :create,
93+
params: { email: @new_user.email,
94+
redirect_url: @redirect_url },
95+
xhr: true
96+
@resource = assigns(:resource)
97+
98+
@mail = ActionMailer::Base.deliveries.last
99+
@token, @client_config = token_and_client_config_from(@mail.body)
100+
end
101+
102+
test 'user should not be confirmed' do
103+
assert_nil @resource.confirmed_at
104+
end
105+
106+
test 'should generate raw token' do
107+
assert @token
108+
assert_equal @new_user.confirmation_token, @token
109+
end
110+
111+
test 'user should receive confirmation email' do
112+
assert_equal @resource.email, @mail['to'].to_s
113+
end
114+
115+
end
89116
end
90117

91118
describe 'failure' do
@@ -96,6 +123,18 @@ def token_and_client_config_from(body)
96123
@resource = assigns(:resource)
97124
refute @resource.confirmed?
98125
end
126+
127+
test 'bad request on resend confirmation' do
128+
post :create, params: { email: nil }, xhr: true
129+
130+
assert_equal 400, response.status
131+
end
132+
133+
test 'user should not be found on resend confirmation request' do
134+
post :create, params: { email: 'bogus' }, xhr: true
135+
136+
assert_equal 404, response.status
137+
end
99138
end
100139
end
101140

0 commit comments

Comments
 (0)