Ruby on Rails
HowToQuicklyDoAuthenticationWithLoginGenerator (Version #78)

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("id")
      ) WITH OIDS;
    
    sqlite:
      CREATE TABLE 'users' (
        'id' INTEGER PRIMARY KEY NOT NULL,
        'login' VARCHAR(80) DEFAULT NULL,
        'password' VARCHAR(40) DEFAULT NULL
      );
    
    • or using rake migrate
      
      class CreateUsers < ActiveRecord::Migration
        def self.up
        create_table :users do |t|
        t.column :login, :string, :default => nil
        t.column :password, :string, :default => nil
        end
        end
      
        def self.down
        drop_table :users
        end
      end
      

If you want to add a default user make sure to set password_confirmation 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
    end
    
  2. 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']
    
  3. 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
    
  4. 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
    
  5. 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:

  • lib/login_system.rb (“user” to “my_model”)
  • app/controllers/your_controller.rb (“user” to “my_model” and “User” to “MyModel”)
  • app/controllers/application.rb (change the model from “user” to “my_model”)
  • app/models/user.rb(rename from “user.rb” to “my_model.rb” and change “User” to “MyModel” and “user” to “my_model”)
  • app/views/login.rhtml (“user_” to “my_model_”)
  • app/views/signup.rhtml (“user_” to “my_model_”)

category: Howto

Bath and Shower
Fragrance
Gift Sets
Hair Care
Makeup
Men’s Grooming
Shaving and Hair Removal
Skin Care
Tools and Accessories

Computers – Computer Add-Ons
Computers – Desktops
Computers – Handhelds & PDAs
Computers – Notebooks

Baby Diapering
Baby Feeding
For Moms
Baby Furniture
Baby Gear
Baby Gifts
Baby Health & Baby Care
Nursery Décor
Potty Training
Baby Safety
Baby Strollers

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("id")
      ) WITH OIDS;
    
    sqlite:
      CREATE TABLE 'users' (
        'id' INTEGER PRIMARY KEY NOT NULL,
        'login' VARCHAR(80) DEFAULT NULL,
        'password' VARCHAR(40) DEFAULT NULL
      );
    
    • or using rake migrate
      
      class CreateUsers < ActiveRecord::Migration
        def self.up
        create_table :users do |t|
        t.column :login, :string, :default => nil
        t.column :password, :string, :default => nil
        end
        end
      
        def self.down
        drop_table :users
        end
      end
      

If you want to add a default user make sure to set password_confirmation 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
    end
    
  2. 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']
    
  3. 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
    
  4. 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
    
  5. 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:

  • lib/login_system.rb (“user” to “my_model”)
  • app/controllers/your_controller.rb (“user” to “my_model” and “User” to “MyModel”)
  • app/controllers/application.rb (change the model from “user” to “my_model”)
  • app/models/user.rb(rename from “user.rb” to “my_model.rb” and change “User” to “MyModel” and “user” to “my_model”)
  • app/views/login.rhtml (“user_” to “my_model_”)
  • app/views/signup.rhtml (“user_” to “my_model_”)

category: Howto

Bath and Shower
Fragrance
Gift Sets
Hair Care
Makeup
Men’s Grooming
Shaving and Hair Removal
Skin Care
Tools and Accessories

Computers – Computer Add-Ons
Computers – Desktops
Computers – Handhelds & PDAs
Computers – Notebooks

Baby Diapering
Baby Feeding
For Moms
Baby Furniture
Baby Gear
Baby Gifts
Baby Health & Baby Care
Nursery Décor
Potty Training
Baby Safety
Baby Strollers