From the docs –
Devise is a flexible authentication solution for Rails based on Warden. It:
- Is Rack based;
- Is a complete MVC solution based on Rails engines;
- Allows you to have multiple roles (or models/scopes) signed in at the same time;
- Is based on a modularity concept: use just what you really need.
Now let us start with some real stuff 🙂
Create new rails project by running the following command –
rails new devise_install
Now open your GEM file (located at the base root of your folder, look for Gemfile in your app)
Add these two lines in your gemfile –
Now navigate to your app folder and run following command –
Un comment bcrypt-ruby in gem file to use Active mode has_secure_password
Once you are done, run the following command –
rails generate devise:install
This command will generate an initializr to describe all devise configuration options
You MUST read those points before proceeding further.
Now we are ready to use it with MODEL.
Run the following command –
rails generate devise User
It will create a User model if not exists previously.
You will see some new files generated after the command. To this point I am interested in two files only one is under
db/migrate folder i.e
20130321042152_devise_create_users.rb (in my case) and another one in
20130321042152_devise_create_users.rb to something like this –
And your user.rb file will go something like this –
Finally run this command –
Once all done go to
http://localhost:3000/users/sign_in and you will see the devise magic 🙂
NOTE – Do not forget to START your server and if you are facing some trouble with rail routes un comment the following line in your routes.rb file