SimplestAuth: Gem-ified, with DataMapper Support

Tony Pitale, Former Viget

Article Category: #Code

Posted on

A few months ago we released simplest_auth as a stripped-down alternative to authentication plugins such as restful auth. After using it in a few projects we came to the realization that there was no particular reason for it to be a plugin and even less of a reason for it to work only with ActiveRecord.

As a result, the latest version on Github is now built as a gem and can be used in a Rails project that is using either ActiveRecord or DataMapper as its ORM. The same example usage from the README applies to ActiveRecord, but I'd like to give an example which uses DataMapper for the model.

 class User include DataMapper::Resource include SimplestAuth::Model property :id, Serial property :email, String, :nullable => false property :crypted_password, String, :length => 60 # validate your model as you see fit # these are some sane defaults for password validates_present :password, :if => :password_required? validates_is_confirmed :password, :if => :password_required? end 

Or, using a username instead of an email address just change these lines:

 property :username, String, :nullable => false # add this line when not using the default of email authenticate_by :username 

Everything else should remain the same. Install the latest gem using:

 sudo gem install simplest_auth 

Related Articles