Skip to content

Commit

Permalink
Added functionality to verify email from token (#3992)
Browse files Browse the repository at this point in the history
Minor changes


Redirection of routes


Added verification link in the welcome email


Update is_verified only if user is not already verified


Tests added


fixed tests
  • Loading branch information
shubhscoder authored and jywarren committed Feb 7, 2019
1 parent 6d8e154 commit e731d60
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
15 changes: 15 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,21 @@ def shortlink
end
end

def verify_email
decrypted_user_id = User.validate_token(params[:token])
action_msg = "Email verification failed"
if decrypted_user_id != 0
user_obj = User.find(decrypted_user_id)
if user_obj.is_verified
action_msg = "Email already verified"
else
user_obj.update_column(:is_verified, true)
action_msg = "Successfully verified email"
end
end
redirect_to "/login", flash: { notice: action_msg }
end

private

def set_user
Expand Down
7 changes: 7 additions & 0 deletions app/views/welcome_mailer/notify_newcomer.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@

Looking forward to hearing from you soon,

Please click on the link below to verify your email.

<% verifaction_link="#{ActionMailer::Base.default_url_options[:host]}/verify/#{@user.generate_token}" %>
<%= link_to "#{verifaction_link}", verifaction_link%>

In case you are not able to click the link, try copy pasting the link in the browser.

The Public Lab Community
<% end %>

Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
get 'people/:tagname' => 'users#list'
get 'signup' => 'users#new'
get 'home' => 'home#front'
get 'verify/:token' => 'users#verify_email'
resources :relationships, only: [:create, :destroy]

get '/wiki/:id/comments', to: 'wiki#comments'
Expand Down
14 changes: 14 additions & 0 deletions test/functional/users_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,18 @@ def setup
assert_equal [], UserTag.where(uid: users(:bob).id, value: "notify-comment-indirect:false")
assert_equal [], UserTag.where(uid: users(:bob).id, value: 'digest:digest')
end

test 'upon verification redirection to login takes place' do
test_user = users(:admin)
email_verification_token = test_user.generate_token
get :verify_email, params: { token: email_verification_token }
assert_redirected_to "/login"
end

test 'upon verification the is_verified field gets updated appropriately' do
test_user = users(:admin)
email_verification_token = test_user.generate_token
get :verify_email, params: { token: email_verification_token }
assert_equal "Successfully verified email", flash[:notice]
end
end

0 comments on commit e731d60

Please sign in to comment.