Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickdet/325531 to your computer and use it in GitHub Desktop.
Save patrickdet/325531 to your computer and use it in GitHub Desktop.

I’m working on both learning authlogic and Rails 3. The process here works (given the Rails 3.0.0.beta1 gem) for me. There is a list of the many resources I complied this from at the bottom of the document.

Travelers beware: this is incomplete, and possibly even wrong. But between the stuff about scaffolding and binary logic’s example app you should be able to get off your feet.

Basic Application

We assume Rails 3 is installed. We will create a rails app as usual.

rails authtest
cd authest
rails g scaffold post title:string body:text
rails g scaffold user username:string fullname:string is_admin:boolean

Install Authlogic

We need to install from source, because of the latest Authlogic gem’s current incompatibility with Rails 3.

# ./Gemfile
# ...
gem "authlogic", :git => "git://github.com/binarylogic/authlogic.git"

Now, let bundle do its work

bundle install
bundle pack

Get ready for authentication

Create session, and prepare the user class

rails g migration AddAuthToUser crypted_password:string password_salt:string persistence_token:string
rails g scaffold user_session --migration false --parent=Authlogic::Session::Base

Do the session stuff

h

# app/controllers/application.rb
class ApplicationController < ActionController::Base
  filter_parameter_logging :password, :password_confirmation
  helper_method :current_user_session, :current_user

  private
    def current_user_session
      return @current_user_session if defined?(@current_user_session)
      @current_user_session = UserSession.find
    end

    def current_user
      return @current_user if defined?(@current_user)
      @current_user = current_user_session && current_user_session.user
    end
end

Use It

Further Reading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment