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:
- And then add a method to the User model that generates and stores new tokens:
- Next, you need to create a before_filter that checks for the presence of a token and handles it:
- 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: