Skip to content

Commit

Permalink
Fix invalid and dummy token issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashish944 committed Jul 15, 2020
1 parent 1dcf2ce commit 4d3bea6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
41 changes: 31 additions & 10 deletions app/controllers/light/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,27 @@ def sendtest
end

def unsubscribe
unless(@user.is_subscribed)
@message = 'You have already unsubscribed!!'
else
if @user.present? && @user.sidekiq_status == 'Subscribed'
@user.update(is_subscribed: 'false',
unsubscribed_at: DateTime.now,
sidekiq_status: 'Unsubscribed')
unsubscribed_at: DateTime.now,
sidekiq_status: 'Unsubscribed')
@message = 'Unsubscribed successfully!!'
else
@message = response_message('unsubscribed')
end
end

def subscribe
@user.update(is_subscribed: 'true',
sidekiq_status: 'Subscribed',
subscribed_at: DateTime.now,
remote_ip: request.remote_ip,
user_agent: request.env['HTTP_USER_AGENT'])
if @user.present? && @user.sidekiq_status == 'Unsubscribed'
@user.update(is_subscribed: 'true',
sidekiq_status: 'Subscribed',
subscribed_at: DateTime.now,
remote_ip: request.remote_ip,
user_agent: request.env['HTTP_USER_AGENT'])
@message = 'Subscribed successfully!!'
else
@message = response_message('subscribed')
end
end

def sendmailer
Expand Down Expand Up @@ -140,5 +145,21 @@ def users_params
def user_with_token
@user = Light::User.where(token: params[:token]).first
end

def dummy_token?
params[:token] == 'test_user_dummy_id'
end

def response_message(status)
if dummy_token?
"#{status.capitalize} successfully!!"
elsif @user.nil?
"Hey, it seems request you are trying to access is invalid. If you have any " +
"concerns about our newsletter's subscription, kindly get in touch with " +
"<a href='mailto:[email protected]' class='email'>[email protected]</a>"
else
"You have already #{status}!!"
end
end
end
end
3 changes: 3 additions & 0 deletions app/helpers/light/users_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module Light
module UsersHelper
def dummy_token?
params[:token] == 'test_user_dummy_id'
end
end
end
7 changes: 4 additions & 3 deletions app/views/light/users/subscribe.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
%h2
%center
Subscribed successfully!!
= @message.html_safe
%br
To unsubscribe again
= link_to "CLICK HERE !!", main_app.users_unsubscribe_path(token: @user.token)
- if @user.present? || dummy_token?
To unsubscribe again
= link_to "CLICK HERE !!", main_app.users_unsubscribe_path(token: @user.try(:token) || params[:token])

7 changes: 4 additions & 3 deletions app/views/light/users/unsubscribe.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
%h2
%center
= @message
= @message.html_safe
%br
Would you like to subscribe again?
= link_to "CLICK HERE !!", main_app.users_subscribe_path(token: @user.token)
- if @user.present? || dummy_token?
Would you like to subscribe again?
= link_to "CLICK HERE !!", main_app.users_subscribe_path(token: @user.try(:token) || params[:token])

0 comments on commit 4d3bea6

Please sign in to comment.