Skip to content

Instantly share code, notes, and snippets.

@tbonemalone
Last active January 3, 2016 00:59
Show Gist options
  • Save tbonemalone/8386123 to your computer and use it in GitHub Desktop.
Save tbonemalone/8386123 to your computer and use it in GitHub Desktop.
Quick notes on rolling quick authentication for a simple rails app. Crat

This is a distilation for how to roll a simple authentication system in Rails 3.

Excellent Sources:
RailsTutorials.org
RailsCasts Episode #250

  1. Add bcrypt-ruby gem and bundle

  2. Generate controller

    1. rails g controller users
    • Note: controllers are named plural by convention
  3. Generate model

    1. rails g model user name:string email:string
    2. bundle exec rake db:migrate
    3. Set up validations
      • validates :name, presence: true
      • validates :email, presence: true, uniqueness: { case_insensitive: false }
    • Note: models are named singular by convention
    1. Ensure uniqueness of each user
      • Add index to User model (helps with uniquness)
        1. Run rails g migration add_index_to_users_email
        2. In generate migration file add line: add_index :users, :email, unique: true
        3. bundle exec db:migrate
        4. In models/user.rb add the line: before_save { |user| user.email = email.downcase }
  4. Adding secure/encrypted password

    1. Add password_digets to User db
      • Run rails g migration add_password_digest_to_users password_digest:string
      • Then db:migrate bundle exec rake db:migrate
  5. Setting up user authentication

    1. If not installed install bcrypt gem
    2. Make sure password and probably password_confirmation are accessible in modles/user.rb
      • attr_accessible ... :password, :password_confirmation
    3. Requre presence of password in model
      • validates :password, presence: true, set a min length validation if desired
      • validates :password_confirmation, presence: true
  6. Configure routes to handle users resources. resources :users

  7. Create any view that you need, show, new etc.

  8. Write actions for signing up users new and create

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