Ruby on Rails - rails find_by - Automatically Authenticate Email Login Tokens with Rails & Devise - ruby on rails tutorial - rails guides - rails tutorial - ruby rails
How to build automatic email login tokens?
- Automatic login links can be implemented by inserting a per-user secret token into the URL parameters.
- The back-end system to this can be simple or very complex, depending on your requirements.
- The simplest solution was a good place to start: only the last email’s login link would work, and adding the token to links in emails would not be automatic.
- You need to add one column token to our User model:
rails g migration AddTokenToUser token:string
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
- And then add a method to the User model that generates and stores new tokens:
def new_token!
SecureRandom.hex(16).tap do |random_token|
update_attributes token: random_token
Rails.logger.info("Set new token for user #{ id }")
end
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
- Next, you need to create a before_filter that checks for the presence of a token and handles it:
def maybe_login_from_token
Rails.logger.info "maybe_login_from_token: '#{ params[:token] }'"
return if (token = params[:token]).blank?
if (user = User.find_by_token(token))
Rails.logger.info "One time login token used for user #{ user.id }"
sign_in(user)
else
Rails.logger.info "No user found from token: '#{ token }'"
end
# strip token regardless of success
redirect_to request.path,
params.except(:token, :action, :controller)
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
- Here, you’ve got a barely-minimum viable product: from a Rails console, you can create a token for a user and then construct a link to log in as that user.
- Actually, hooking the login token into select links is very simple. Generate a new token in the mailer and include the token as a URL parameter in the template:
diff --git a/app/mailers/reservation_mailer.rb b/app/mailers/reservation_mailer.rb
index 4ee2db4..0f8faf2 100644
--- a/app/mailers/reservation_mailer.rb
+++ b/app/mailers/reservation_mailer.rb
@@ -16,6 +16,8 @@ class ReservationMailer < CameraLendsMailer
def reservation_request_lender(reservation)
@reservation = reservation
+ @owner = reservation.item.owner
+ @owner.new_token!
item = reservation.item.decorate
mail to: reservation.item.owner.email_for_mailer,
subject: "Reservation request for #{ item.display_name }"
diff --git a/app/views/reservation_mailer/reservation_request_lender.html.haml b/app/views/reservation_mailer/reservation_request_lender.html.haml
diff --git a/app/mailers/reservation_mailer.rb b/app/mailers/reservation_mailer.rb
index 4ee2db4..0f8faf2 100644
--- a/app/mailers/reservation_mailer.rb
+++ b/app/mailers/reservation_mailer.rb
@@ -16,6 +16,8 @@ class ReservationMailer < CameraLendsMailer
def reservation_request_lender(reservation)
@reservation = reservation
+ @owner = reservation.item.owner
+ @owner.new_token!
item = reservation.item.decorate
mail to: reservation.item.owner.email_for_mailer,
subject: "Reservation request for #{ item.display_name }"
diff --git a/app/views/reservation_mailer/reservation_request_lender.html.haml b/app/views/reservation_mailer/reservation_request_lender.html.haml