Saturday, 2 July 2016

Rails 4 OmniAuth using Devise with Gmail, Facebook and Linkedin

Devise 

About Devise

Devise is a flexible authentication solution for Rails based on Warden. 
  • Is Rack based;
  • Is a complete MVC solution based on Rails engines
  • Allows you to have multiple models signed in at the same time;
  • Is based on a modularity concept: use only what you really need.
It's composed of 10 modules:
  • Database Authenticatable: hashes and stores a password in the database to validate the authenticity of a user while signing in. The authentication can be done both through POST requests or HTTP Basic Authentication.
  • Omniauthable: adds OmniAuth (https://github.com/intridea/omniauth) support.
  • Confirmable: sends emails with confirmation instructions and verifies whether an account is already confirmed during sign in.
  • Recoverable: resets the user password and sends reset instructions.
  • Registerable: handles signing up users through a registration process, also allowing them to edit and destroy their account.
  • Rememberable: manages generating and clearing a token for remembering the user from a saved cookie.
  • Trackable: tracks sign in count, timestamps and IP address.
  • Timeoutable: expires sessions that have not been active in a specified period of time.
  • Validatable: provides validations of email and password. It's optional and can be customized, so you're able to define your own validations.
  • Lockable: locks an account after a specified number of failed sign-in attempts. Can unlock via email or after a specified time period. 

Getting Started

Step 1: add following line to gem file
    gem "devise"
  

Step 2: Console
    bundle install
  
Step 3:
    rails g devise:install
  
Step 4:
    config.action_mailer.default_url_options = { host: 'localhost:3000' }
  
Step 5:
following command you will replace MODEL with the class name used for the application's users (it's frequently User but could be any name).
    rails g devise MODEL
  
Step 6:
    rake db:migrate
  
Step 7: Controllers filters and helpers
Devise will create some helpers to use inside your controller and views. To set up a controller with the user authentication. just add this before_action( assume your devise model is 'User')
    before_action: authenticate_user!
  

 


No comments:

Post a Comment