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
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

This ruby on rails tutorial page provides you the following key areas such as ruby , rail , ruby on rails , rail forum , ruby on rails tutorial , ruby tutorial , rails guides , rails tutorial , learn ruby , rails form_for , ruby rails , ruby class , what is ruby on rails , rails installer , ruby online , learn ruby on rails , ruby on rails jobs , rails find_by , install rails , easyrail , rubyonrails , link_to rails , ruby on rails developer , learn ruby the hard way , railscasts , ruby on rails examples , ruby on rails vs php , rails 4 , rails activerecord , rails generate , ruby and rails , ruby on rails download , install ruby on rails , ruby net http , what is rails , ruby app , ruby vs ruby on rails , ruby on rails windows , rails for zombies , ruby on rails book , ruby on rails development , ruby on rails ide , ruby on rails tutorial pdf

Related Searches to Automatically Authenticate Email Login Tokens with Rails & Devise