<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>The Napkin ~ A Blog By Highgroove Studios comments on Auto-Login for Any URL in Rails</title>
    <link>http://napkin.highgroove.com/</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>The Napkin ~ A Blog By Highgroove Studios comments</description>
    <item>
      <title>"Auto-Login for Any URL in Rails": comment by Eliana</title>
      <description>&lt;p&gt;Great code! very useful for automatic logins in websites. I especially need this because I&amp;#8217;m working with a handful of people. Very useful for their email accounts.&lt;/p&gt;</description>
      <pubDate>Thu,  7 Feb 2008 01:13:32 EST</pubDate>
      <guid>http://napkin.highgroove.com/articles/2008/02/06/auto-login-for-any-url-in-rails#comment-1001</guid>
      <link>http://napkin.highgroove.com/articles/2008/02/06/auto-login-for-any-url-in-rails#comment-1001</link>
    </item>
    <item>
      <title>"Auto-Login for Any URL in Rails" by james</title>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;To get around that I modified the application to accept URL's like the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;http://domain.com/login/TOKEN/ANY/SITE/URL
&lt;/code&gt;&lt;/pre&gt;

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

&lt;p&gt;The code is easy enough too.  I imagine many of us have a sessions controller that looks something like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class SessionController &amp;lt; ApplicationController
  def create
    if user = User.authenticate(params[:email], params[:pass])
      # log user in...
    else
      # login error message...
    end
  end

  # ...
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;First, I just added some support for the token based login with redirect to that:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class SessionController &amp;lt; 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
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The magic &lt;code&gt;redirect_to()&lt;/code&gt; 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:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# a custom login route with forwarding
map.connect "login/:token/*path", :controller =&amp;gt; "session",
                                  :action     =&amp;gt; "create"
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;*path&lt;/code&gt; 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 &lt;code&gt;path&lt;/code&gt;, so just remember that you need to rejoin the elements to make them a real URL again.&lt;/p&gt;

</description>
      <pubDate>Wed,  6 Feb 2008 20:11:00 EST</pubDate>
      <guid>&lt;a href="/articles/2008/02/06/auto-login-for-any-url-in-rails"&gt;Auto-Login for Any URL in Rails&lt;/a&gt;</guid>
      <link>&lt;a href="/articles/2008/02/06/auto-login-for-any-url-in-rails"&gt;Auto-Login for Any URL in Rails&lt;/a&gt;</link>
    </item>
  </channel>
</rss>
