Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resend confirmation instructions #1267

Merged
merged 3 commits into from Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 27 additions & 3 deletions app/controllers/devise_token_auth/confirmations_controller.rb
Expand Up @@ -2,6 +2,7 @@

module DeviseTokenAuth
class ConfirmationsController < DeviseTokenAuth::ApplicationController

def show
@resource = resource_class.confirm_by_token(params[:confirmation_token])

Expand All @@ -10,9 +11,6 @@ def show

redirect_header_options = { account_confirmation_success: true }

# give redirect value from params priority or fall back to default value if provided
redirect_url = params[:redirect_url] || DeviseTokenAuth.default_confirm_success_url

if signed_in?(resource_name)
client_id, token = signed_in_resource.create_token

Expand All @@ -30,5 +28,31 @@ def show
raise ActionController::RoutingError, 'Not Found'
end
end

def create
return head :bad_request if params[:email].blank?

bettysteger marked this conversation as resolved.
Show resolved Hide resolved
@resource = resource_class.dta_find_by(uid: params[:email].downcase, provider: provider)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perfect 👌


return head :not_found unless @resource

@resource.send_confirmation_instructions({
redirect_url: redirect_url,
client_config: params[:config_name]
})

head :ok
end

private

# give redirect value from params priority or fall back to default value if provided
def redirect_url
params.fetch(
:redirect_url,
DeviseTokenAuth.default_confirm_success_url
)
end

end
end
Expand Up @@ -86,6 +86,33 @@ def token_and_client_config_from(body)
assert response.body.include?('account_confirmation_success')
end
end

describe 'resend confirmation' do
before do
post :create,
params: { email: @new_user.email,
redirect_url: @redirect_url },
xhr: true
@resource = assigns(:resource)

@mail = ActionMailer::Base.deliveries.last
@token, @client_config = token_and_client_config_from(@mail.body)
end

test 'user should not be confirmed' do
assert_nil @resource.confirmed_at
end

test 'should generate raw token' do
assert @token
assert_equal @new_user.confirmation_token, @token
end

test 'user should receive confirmation email' do
assert_equal @resource.email, @mail['to'].to_s
end

end
end

describe 'failure' do
Expand All @@ -96,6 +123,18 @@ def token_and_client_config_from(body)
@resource = assigns(:resource)
refute @resource.confirmed?
end

test 'bad request on resend confirmation' do
post :create, params: { email: nil }, xhr: true

assert_equal 400, response.status
end

test 'user should not be found on resend confirmation request' do
post :create, params: { email: 'bogus' }, xhr: true

assert_equal 404, response.status
end
end
end

Expand Down