Big Nerd Ranch Logo • • •
  • work
  • team training
  • bootcamps
  • books
  • screencasts
  • resources
  • blog
  • work
  • team training
  • bootcamps
  • books
  • screencasts
  • resources
  • blog

Auto-Login for Any URL in Rails

James Edward Gray
James Edward Gray
Feb 6, 2008 • Back-End
  • Twitter
  • Facebook
  • LinkedIn
  • Reddit
  • Email

One of our current projects at Highgroove sends a lot of email to its users. It essentially walks them through a process and emails them at each step. All of those messages include URL’s to visit the relevant page in the application for that step. Since we’ve emailed them the URL’s we don’t want them to have to login every time they click one.

To get around that I modified the application to accept URL’s like the following:

    http://domain.com/login/TOKEN/ANY/SITE/URL 

These URL’s log the user in using their security TOKEN and then redirect them to /ANY/SITE/URL. This setup allows me to easily forward a user to any URL on the site which is great when writing all of these emails.

The code is easy enough too. I imagine many of us have a sessions controller that looks something like:

    class SessionController < ApplicationController   def create     if user = User.authenticate(params[:email], params[:pass])       # log user in...     else       # login error message...     end   end    # ... end 

First, I just added some support for the token based login with redirect to that:

    class SessionController < ApplicationController   def create     if params[:token] and (user = User.find_by_token(params[:token]))       # log user in...       if params[:path].is_a? Array         redirect_to "/#{params[:path].join('/')}"       else         redirect_to home_path  # or whatever default page you want       end     elsif user = User.authenticate(params[:email], params[:pass])       # log user in...     else       # login error message...     end   end    # ... end 

The magic redirect_to() call in that new code uses a not-often-seen feature of Rails’s routing. You can specify that Rails collect any number of trailing URL bits into an Array much like Ruby can do for method parameters. Here’s the route definition I am using to get users to the code above:

    # a custom login route with forwarding map.connect "login/:token/*path", :controller => "session",                                   :action     => "create" 

The *path is the magic slurping parameter syntax, again just like arguments to a Ruby method. Rails will collect each piece of the remaining URL into an Array called path, so just remember that you need to rejoin the elements to make them a real URL again.

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit
  • Email
James Edward Gray
more by
James Edward Gray

Big Nerd Ranch Back-End Courses

Interested in learning more about our basic and advanced Back-End Courses?
Learn from the experts at a Big Nerd Ranch Bootcamp!

LEARN MORE

Big Nerd Ranch Screencasts

Interested in leveling up your coding skills from the same authors of the Big Nerd Ranch Guide? Subscribe to The Frontier today!

LEARN MORE

Related Posts:

Looking Forward to KotlinConf 2018

David Greenhalgh

JSConf US and the Expanding Web

Anna Sedlar

JavaScript Project Configuration

Devon Bull

Managing Front-End Assets in Vapor, Part 3: Concatenating Files

Josh Justice

Managing Front-End Assets in Vapor, Part 2: Using a Package Manager

Josh Justice

Recent Comments

comments powered by Disqus
  • Twitter
  • Facebook
  • Instagram
  • Github
  • LinkedI
  • Youtube
Subscribe to Our Newsletter

App Development

  • Work

Books

  • All books
  • Front-end
  • Swift
  • iOS
  • Android

Team Training

  • Team Training Overview
  • iOS
  • Android
  • Web
  • Design

Bootcamps

  • Bootcamp Overview
  • iOS
  • Android
  • Web
  • Schedule
  • Georgia Ranch
  • Sign In

Company

  • Who We Are
  • News
  • Resources
  • Careers
  • Blog
  • Contact

Copyright© 1998 - 2019 Big Nerd Ranch, LLC. All Rights Reserved. | Privacy Policy