Ruby on Rails
HowToQuicklyDoAuthenticationWithLoginGenerator

This no longer works for rails version 2.0.2. An update for this generator is necessary

  1. Install the LoginGenerator gem:
    gem install login_generator
    
  2. Generate a login system, we’ll call it “Account1
    ruby script/generate login Account
    
  3. Make a table to store users; choose the flavor that suits your needs:
    mysql syntax:
      CREATE TABLE users (
        id int(11) NOT NULL auto_increment,
        login varchar(80) default NULL,
        password varchar(40) default NULL,
        PRIMARY KEY  (id)
      );

postgres :
CREATE TABLE “users” (
“id” SERIAL NOT NULL UNIQUE,
“login” TEXT,
“password” TEXT,
PRIMARY KEY
) WITH OIDS;

sqlite:
CREATE TABLE ‘users’ (
‘id’ INTEGER PRIMARY KEY NOT NULL,
‘login’ VARCHAR DEFAULT NULL,
‘password’ VARCHAR DEFAULT NULL
);

def self.down drop_table :users end

end

If you want to add a default user make sure to set passwordconfirmation in the migration_ (add this line in self.up)


User.create(:login => 'admin',
  :password => 'password',
  :password_confirmation => 'password')
  1. Edit app/controllers/application.rb, and make it look something like this:
    require 'login_system'

class ApplicationController < ActionController::Base
include LoginSystem
model :user
endvi

If you’re using Rails > 2.0, don’t include the model line

  1. Edit app/controllers/account_controller.rb to set who if anyone can delete users. Until they have admin roles, most people can set it so only account holders can delete their account:
    
    def delete
      if params['id'] && @session['user'] && @session['user'].id == params['id']
    
  2. Edit your own controllers now, and add the line
    before_filter :login_required
    

    to the inside of the class so that it ends up looking something like this:
    class AllMySecretsController < ApplicationController
       before_filter :login_required
       
       def show_one_secret
          ...
       end
    end
    
  3. Of course, I sometimes don’t want every method hidden behind the iron curtain, so I can exclude some of them (such as show_one_secret above) from being protected like so:
    class AllMySecretsController < ApplicationController
       before_filter :login_required, :except => [ :show_one_secret ]
       
       def show_one_secret
          ...
       end
    end
    
  4. Now, we’re all eager to actually add users too, so let’s go ahead and do that. Point your browser to:
    http://myrails.foobar.com/account/signup

and voila! you can start adding users.

Much info taken from Login Generator’s README_LOGIN
——

1 You can call the login controller whatever you want (Account is just an example). However to change the login system to use a model other than “User” (for instance "MyModel") you will need to modify the following:

category: Howto