<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type='text/xsl' href='http://gaurav-scr.spaces.live.com/mmm2008-07-24_12.50/rsspretty.aspx?rssquery=en-US;http%3a%2f%2fgaurav-scr.spaces.live.com%2fcategory%2fRails%2ffeed.rss' version='1.0'?><rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:msn="http://schemas.microsoft.com/msn/spaces/2005/rss" xmlns:live="http://schemas.microsoft.com/live/spaces/2006/rss" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Blog on Rails: Rails</title><description /><link>http://gaurav-scr.spaces.live.com/?_c11_BlogPart_BlogPart=blogview&amp;_c=BlogPart&amp;partqs=catRails</link><language>en-US</language><pubDate>Mon, 18 Aug 2008 01:46:44 GMT</pubDate><lastBuildDate>Mon, 18 Aug 2008 01:46:44 GMT</lastBuildDate><generator>Microsoft Spaces v1.1</generator><docs>http://www.rssboard.org/rss-specification</docs><ttl>60</ttl><cf:parentRSS>http://gaurav-scr.spaces.live.com/blog/feed.rss</cf:parentRSS><live:type>blogcategory</live:type><live:identity><live:id>5269363790095024524</live:id><live:alias>gaurav-scr</live:alias></live:identity><cf:listinfo><cf:group ns="http://schemas.microsoft.com/live/spaces/2006/rss" element="typelabel" label="Type" /><cf:group ns="http://schemas.microsoft.com/live/spaces/2006/rss" element="tag" label="Tag" /><cf:group element="category" label="Category" /><cf:sort element="pubDate" label="Date" data-type="date" default="true" /><cf:sort element="title" label="Title" data-type="string" /><cf:sort ns="http://purl.org/rss/1.0/modules/slash/" element="comments" label="Comments" data-type="number" /></cf:listinfo><item><title>Using authentication in Rails</title><link>http://gaurav-scr.spaces.live.com/Blog/cns!49208A72E4EB818C!155.entry</link><description>The easiest way to authenticate in rails is by using the before_filter in you controller.&lt;br&gt;&lt;br&gt;Add this code in the top of your controller.&lt;br&gt;&lt;br&gt;--------------------------------------------------------------------------------------------------------------&lt;br&gt;&lt;span style="font-style:italic"&gt;# before_filter calls the supplied function &lt;/span&gt;&lt;br style="font-style:italic"&gt;&lt;span style="font-style:italic"&gt;# before calling any other actions in the current controller &lt;/span&gt;&lt;br style="font-style:italic"&gt;&lt;span style="font-style:italic"&gt;# except the actions give in the 'except' hash&lt;/span&gt;&lt;br style="font-style:italic"&gt;&lt;br style="font-style:italic"&gt;&lt;span style="font-style:italic"&gt;before_filter :check_authentication, :except =&amp;gt; [:sign_up, :login]&lt;/span&gt;&lt;br style="font-style:italic"&gt;&lt;br style="font-style:italic"&gt;&lt;span style="font-style:italic"&gt;#  Authenticating the user&lt;/span&gt;&lt;br style="font-style:italic"&gt;&lt;span style="font-style:italic"&gt;def check_authentication&lt;/span&gt;&lt;br style="font-style:italic"&gt;&lt;span style="font-style:italic"&gt;  unless session[:user_id]&lt;/span&gt;&lt;br style="font-style:italic"&gt;&lt;span style="font-style:italic"&gt;    session[:original_uri] = request.request_uri&lt;/span&gt;&lt;br style="font-style:italic"&gt;&lt;span style="font-style:italic"&gt;    flash_error('Please Login first','user','login')&lt;/span&gt;&lt;br style="font-style:italic"&gt;&lt;span style="font-style:italic"&gt;  end&lt;/span&gt;&lt;br style="font-style:italic"&gt;&lt;span style="font-style:italic"&gt;end&lt;/span&gt;&lt;br&gt;--------------------------------------------------------------------------------------------------------------&lt;br&gt;&lt;br&gt;This code checks if there is a variable named user_id in the session.&lt;br&gt;If such a variable is not present the it redirects the user to the login screen and stores the requested url so that when the user logs in he is redirected to this stored location.&lt;br&gt;&lt;br&gt;The following variable shows how we check the user's password&lt;br&gt;&lt;br&gt;--------------------------------------------------------------------------------------------------------------&lt;br&gt;&lt;span style="font-style:italic"&gt;def login&lt;/span&gt;&lt;br style="font-style:italic"&gt;&lt;span style="font-style:italic"&gt;  flash_notice('You are already logged in','user','index')  if session[:user_id]&lt;/span&gt;&lt;br style="font-style:italic"&gt;&lt;span style="font-style:italic"&gt;  if request.post?  &lt;/span&gt;&lt;br style="font-style:italic"&gt;&lt;span style="font-style:italic"&gt;    user = User.find_by_name(params[:user_name])&lt;/span&gt;&lt;br style="font-style:italic"&gt;&lt;span style="font-style:italic"&gt;    if user.authenticate?(params[:password])&lt;/span&gt;&lt;br style="font-style:italic"&gt;&lt;span style="font-style:italic"&gt;      session[:user_id] = user.id&lt;/span&gt;&lt;br style="font-style:italic"&gt;&lt;span style="font-style:italic"&gt;      flash_notice('Successfully logged in','user','index')&lt;/span&gt;&lt;br style="font-style:italic"&gt;&lt;span style="font-style:italic"&gt;    else&lt;/span&gt;&lt;br style="font-style:italic"&gt;&lt;span style="font-style:italic"&gt;      flash_notice('Please check your username and password','user','login')&lt;/span&gt;&lt;br style="font-style:italic"&gt;&lt;span style="font-style:italic"&gt;    end&lt;/span&gt;&lt;br style="font-style:italic"&gt;&lt;span style="font-style:italic"&gt;  end&lt;/span&gt;&lt;br style="font-style:italic"&gt;&lt;span style="font-style:italic"&gt;end&lt;/span&gt;&lt;br&gt;--------------------------------------------------------------------------------------------------------------&lt;br&gt;&lt;br&gt;In the first line of the above given action we check if the user is already logged in.&lt;br&gt;Then we check for the post request. Then we find the user and authenticate him.&lt;br&gt;If the user's password is successfully matched the we store the user's id in the session so that it can always pass the check_authentication function.&lt;br&gt;&lt;br&gt;This is the simplest possible authentication that can be used in rails.&lt;br&gt;&lt;img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=5269363790095024524&amp;page=RSS%3a+Using+authentication+in+Rails&amp;referrer=" width="1px" height="1px" border="0" alt=""&gt;&lt;img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&amp;amp;NA=1149&amp;amp;PI=73329&amp;amp;RF=&amp;amp;DI=3919&amp;amp;PS=85545&amp;amp;TP=gaurav-scr.spaces.live.com&amp;amp;GT1=gaurav-scr"&gt;</description><comments>http://gaurav-scr.spaces.live.com/Blog/cns!49208A72E4EB818C!155.entry#comment</comments><guid isPermaLink="true">http://gaurav-scr.spaces.live.com/Blog/cns!49208A72E4EB818C!155.entry</guid><pubDate>Fri, 08 Dec 2006 13:22:30 GMT</pubDate><slash:comments>0</slash:comments><msn:type>blogentry</msn:type><live:type>blogentry</live:type><live:typelabel>Blog entry</live:typelabel><wfw:commentRss>http://gaurav-scr.spaces.live.com/blog/cns!49208A72E4EB818C!155/comments/feed.rss</wfw:commentRss><wfw:comment>http://gaurav-scr.spaces.live.com/Blog/cns!49208A72E4EB818C!155.entry#comment</wfw:comment><dcterms:modified>2006-12-08T13:22:30Z</dcterms:modified></item></channel></rss>