what is devise ? and how to use devise gem

 


 Q => What is the devise And how to use devise gem in rails ?

Devise is a well known solution for authentication in Rails applications. It's full featured (it not only adds authentication but also password recovery, email changing, session timeout, locking, ip tracking, etc.) and can be expanded to add even more (like JWT authentication).



Create a new Rails project:


   rails new Demo


Add the Devise gem to your Gem file:


   gem 'devise'



Run this commend to install the gem.
   

   $ bundle install


Run the Devise installation generator:


  rails generate devise:install


 Make sure you have defined default url options in your environments files. Open up config/environments/development.rb and add this line:


 
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }



Generate a User model:


rails generate devise User


Run database migrations:


rails db:migrate


Customize the Devise views:


rails generate devise:views



Controller filters and helpers

Devise will create some helpers to use inside your controllers and views. To set up a controller with user authentication, just add this before_action (assuming your devise model is 'User'):


before_action :authenticate_user!


To verify if a user is signed in, use the following helper:


user_signed_in?


For the current signed-in user, this helper is available:

current_user


You can access the session for this scope:


user_session

Comments

Popular posts from this blog

Rails Create new Project

Ruby Variable