<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Scott Blaine &#187; authentication</title>
	<atom:link href="http://scottblaine.com/tag/authentication/feed" rel="self" type="application/rss+xml" />
	<link>http://scottblaine.com</link>
	<description>Omaha web developer</description>
	<lastBuildDate>Wed, 12 Oct 2011 01:00:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>How to use additional one-to-one models with restful_authentication</title>
		<link>http://scottblaine.com/how-to-use-additional-one-to-one-models-with-restful_authentication</link>
		<comments>http://scottblaine.com/how-to-use-additional-one-to-one-models-with-restful_authentication#comments</comments>
		<pubDate>Sun, 09 Aug 2009 01:00:03 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[models]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[restful_authentication]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://scottblaine.com/?p=345</guid>
		<description><![CDATA[Restful_authentication collects some basic information to get an account set up, like a username, email address, and password, which all gets stored in a User model. What if you have some additional information that you&#8217;d like to collect upon registration, and you don&#8217;t want to store it in the User model (addresses, for example)? Rails [...]]]></description>
			<content:encoded><![CDATA[<p>Restful_authentication collects some basic information to get an account set up, like a username, email address, and password, which all gets stored in a User model. What if you have some additional information that you&#8217;d like to collect upon registration, and you don&#8217;t want to store it in the User model (addresses, for example)? Rails 2.3 makes this easy.</p>
<p>Let&#8217;s get an example registration app set up:</p>
<pre>
rails registration
ruby script/plugin install http://svn.techno-weenie.net/projects/plugins/restful_authentication
ruby script/generate authenticated user sessions
</pre>
<p>Cool, now you have your registration app set up with a Users model, controller, and view. If you start up your server and head to http://localhost:3000/users/new/ you should see a very basic registration form. Let&#8217;s add our Address model. For the sake of brevity, I&#8217;m just going to create a field that references the User model and one field for a zip code, but you get the idea.</p>
<pre>
ruby script/generate model Address user:references zipcode:string
rake db:migrate
</pre>
<p>With our Address model in place, we need to let the User model know that it is linked to the Address model. Here&#8217;s what the User model looks like (\app\models\user.rb):</p>
<pre>
require 'digest/sha1'
class User &lt; ActiveRecord::Base
  # Virtual attribute for the unencrypted password
  attr_accessor :password

  validates_presence_of     :login, :email
  # lots more stuff that we're not going to worry about...
</pre>
<p>We&#8217;re going to add three things:</p>
<pre>
require 'digest/sha1'
class User &lt; ActiveRecord::Base
  has_one :address
  accepts_nested_attributes_for :address
  attr_accessible :address_attributes

  # Virtual attribute for the unencrypted password
  attr_accessor :password

  validates_presence_of     :login, :email
  # lots more stuff that we're not going to worry about...
</pre>
<p>We&#8217;ve told the User model that (1) it has one Address model, (2) that it should save data for Address automagically, and (3) that the address_attributes fields are permitted fields to receive data for (if you read through the code a bit further you&#8217;ll see another attr_accessible line, you could add address_attributes there too).</p>
<p>We need to make one change to the User controller (\controllers\users_controller.rb) to prevent an &#8220;Called id for nil&#8221; error later on. At line 6 you should have an empty new method. We&#8217;re going to create an instance variable for User:</p>
<pre>
  # render new.rhtml
  def new
    @user = User.new
  end
</pre>
<p>Let&#8217;s now take a look at the one User view that restful_auth created (\app\views\users\new.html.erb):</p>
<pre>
&lt;%= error_messages_for :user %&gt;
&lt;% form_for :user, :url =&gt; users_path do |f| -%&gt;
&lt;p&gt;&lt;label for=&quot;login&quot;&gt;Login&lt;/label&gt;&lt;br/&gt;
&lt;%= f.text_field :login %&gt;&lt;/p&gt;

&lt;p&gt;&lt;label for=&quot;email&quot;&gt;Email&lt;/label&gt;&lt;br/&gt;
&lt;%= f.text_field :email %&gt;&lt;/p&gt;

&lt;p&gt;&lt;label for=&quot;password&quot;&gt;Password&lt;/label&gt;&lt;br/&gt;
&lt;%= f.password_field :password %&gt;&lt;/p&gt;

&lt;p&gt;&lt;label for=&quot;password_confirmation&quot;&gt;Confirm Password&lt;/label&gt;&lt;br/&gt;
&lt;%= f.password_field :password_confirmation %&gt;&lt;/p&gt;

&lt;p&gt;&lt;%= submit_tag &#039;Sign up&#039; %&gt;&lt;/p&gt;
&lt;% end -%&gt;
</pre>
<p>First of all, we need to change the form_for :user to be form_for @user. Second, we get to add in our zipcode field:</p>
<pre>
&lt;%= error_messages_for :user %&gt;
&lt;% form_for @user, :url =&gt; users_path do |f| -%&gt;
&lt;p&gt;&lt;label for=&quot;login&quot;&gt;Login&lt;/label&gt;&lt;br/&gt;
&lt;%= f.text_field :login %&gt;&lt;/p&gt;

&lt;p&gt;&lt;label for=&quot;email&quot;&gt;Email&lt;/label&gt;&lt;br/&gt;
&lt;%= f.text_field :email %&gt;&lt;/p&gt;

&lt;p&gt;&lt;label for=&quot;password&quot;&gt;Password&lt;/label&gt;&lt;br/&gt;
&lt;%= f.password_field :password %&gt;&lt;/p&gt;

&lt;p&gt;&lt;label for=&quot;password_confirmation&quot;&gt;Confirm Password&lt;/label&gt;&lt;br/&gt;
&lt;%= f.password_field :password_confirmation %&gt;&lt;/p&gt;

&lt;% @user.build_address unless @user.address %&gt;
&lt;% f.fields_for :address do |a| %&gt;
  &lt;p&gt;
    &lt;%= a.label :zipcode %&gt;
    &lt;%= a.text_field :zipcode %&gt;
  &lt;/p&gt;
&lt;% end %&gt;

&lt;p&gt;&lt;%= submit_tag &#039;Sign up&#039; %&gt;&lt;/p&gt;
&lt;% end -%&gt;
</pre>
<p>Here&#8217;s where I ran into the two gotcha&#8217;s. If you don&#8217;t switch form_for :user to form_for @user when you submit the form you&#8217;ll get a very unpleasant error that looks like this:</p>
<pre>
ActiveRecord::AssociationTypeMismatch in UsersController#create
Address(#46729050) expected, got HashWithIndifferentAccess(#23561230)
</pre>
<p>Second, if you don&#8217;t add in line 15, @user.build_address unless @user.address, you&#8217;ll get the following error when you try to view the form:</p>
<pre>You have a nil object when you didn't expect it!</pre>
<p><a href="http://www.pixellatedvisions.com/2009/03/18/rails-2-3-nested-model-forms-and-nil-new-record">More info on why that error occurs here</a>.</p>
<p>Anyway, with the above steps you should now have a registration form that creates records for both User and Address. If you found this helpful, leave a comment and let me know!</p>
]]></content:encoded>
			<wfw:commentRss>http://scottblaine.com/how-to-use-additional-one-to-one-models-with-restful_authentication/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Authentication with CodeIgniter</title>
		<link>http://scottblaine.com/authentication-with-codeigniter</link>
		<comments>http://scottblaine.com/authentication-with-codeigniter#comments</comments>
		<pubDate>Sat, 30 May 2009 01:00:08 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[redux]]></category>

		<guid isPermaLink="false">http://scottblaine.com/?p=140</guid>
		<description><![CDATA[Having finished the registration process, I realized I needed some way to keep people logged in, let them log in, log out, retrieve their password, possibly have some kind of email validation, etc. All sorts of things. I started looking through the CodeIgniter user guide and about they closest things they have is the session [...]]]></description>
			<content:encoded><![CDATA[<p>Having finished the registration process, I realized I needed some way to keep people logged in, let them log in, log out, retrieve their password, possibly have some kind of email validation, etc. All sorts of things. I started looking through the CodeIgniter user guide and about they closest things they have is <a title="CodeIgniter's Session Class" href="http://codeigniter.com/user_guide/libraries/sessions.html">the session library</a>. While you could use it to track if a user is logged in, that&#8217;s about the extent of the value it would provide to you.</p>
<p>At first I looked for ways that I might be able to at least run checks on pages to see if a user is logged in or not. It turns out that&#8217;s not terribly difficult (once you figure out how to do it). You need to <a title="Authentication with CodeIgniter - David Winter" href="http://davidwinter.me.uk/articles/2009/02/21/authentication-with-codeigniter/">create your own authentication controller</a> for use with pages where you want to check if the user is logged in. Cool, that works. I could throw the log in/log out functionality in easily enough, but resetting passwords and validating email addresses would be a whole different matter.</p>
<p>That&#8217;s when I decided that I should start looking for a user-contributed authentication library. The CodeIgniter Wiki has <a title="CodeIgniter Authentication Libraries" href="http://codeigniter.com/wiki/Category:Contributions::Libraries::Authentication/">a nice list of them.</a> As I started looking through them and all their various features I realized I should come up with a list of the features that I needed to have. Here&#8217;s what I was looking for in a library:</p>
<ul>
<li>Good coding practices</li>
<li>Good documentation</li>
<li>Small number of files</li>
<li>Database implementation that&#8217;s not complex</li>
<li>Login using username or email address</li>
<li>Emails for lost passwords</li>
<li>Automatic login</li>
<li>Hashing of passwords</li>
<li>Maximum number of failed login attempts</li>
<li>Emails for activation (nice to have)</li>
<li>reCAPTCHA support (nice to have)</li>
</ul>
<p>Not too harsh, right? Unfortunately most of the libraries ended up being too large, too simple, or completely lacking in documentation. It was hard to find a library that fit somewhere in the middle. I did find one library that might meet all of my requirements, and if not, it&#8217;s awfully close: <a title="reduxauth" href="http://code.google.com/p/reduxauth/">Redux 2 (beta)</a>. It has all of the functionality I want, it doesn&#8217;t contain a crazy number of files, the database is simple and well thought out, there&#8217;s a complete sample application that makes use of the library, and more. I&#8217;m going to give it a try and comment in my next post on how well it works.</p>
<p>I should also mention in the course of my search I came across <a title="What Code Igniter authentication library is best? - Stack Overflow" href="http://stackoverflow.com/questions/346980/what-code-igniter-authentication-library-is-best">an excellent post on Stack Overflow regarding authentication libraries for CodeIgniter</a>. There&#8217;s a lot of great discussion on that page and Redux was also listed there as being one of the better solutions available.</p>
]]></content:encoded>
			<wfw:commentRss>http://scottblaine.com/authentication-with-codeigniter/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

