This is Qiita's first post. It is written by a beginner, so there may be mistakes. If you have any suggestions or advice, I would appreciate it if you could teach me. Thank you.
【environment】
devise is a gem that makes it easy to implement an authentication function in a web application. The authentication function is simply a login and log-out function. Since user information is handled, it is difficult to create from scratch due to security concerns and man-hours. By using devise, the above problems can be solved and the implementation of the authentication function becomes easy.
Describe the following in the Gemfile.
Gemfile
gem 'devise'
After saving, enter the following command to load it into the application.
Terminal
$ bundle install
This completes the installation.
Next, enter the following command to make the initial settings for devise.
Terminal
$ rails g devise:install
If the following screen is displayed, it is successful.
Terminal
      create  config/initializers/devise.rb
      create  config/locales/devise.en.yml
===============================================================================
Depending on your application's configuration some manual setup may be required:
  1. Ensure you have defined default url options in your environments files. Here
     is an example of default_url_options appropriate for a development environment
     in config/environments/development.rb:
       config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
     In production, :host should be set to the actual host of your application.
     * Required for all applications. *
  2. Ensure you have defined root_url to *something* in your config/routes.rb.
     For example:
       root to: "home#index"
     
     * Not required for API-only Applications *
  3. Ensure you have flash messages in app/views/layouts/application.html.erb.
     For example:
       <p class="notice"><%= notice %></p>
       <p class="alert"><%= alert %></p>
     * Not required for API-only Applications *
  4. You can copy Devise views (for customization) to your app by running:
       rails g devise:views
       
     * Not required *
===============================================================================
By the way, when I started using devise, I sometimes skipped this initial setting and got an error ... If that doesn't work, you may want to check if the initial settings have been made.
Create a User model using the devise function. If you want to use a different model name, use the User part as that model name. Note that it will be ** rails g devise model name **, unlike when creating a normal model.
Terminal
$ rails g devise User
If the following screen is displayed, it is successful.
Terminal
    invoke  active_record
    create    db/migrate/20201103032107_devise_create_users.rb
    create    app/models/user.rb
    invoke    test_unit
    create      test/models/user_test.rb
    create      test/fixtures/users.yml
    insert    app/models/user.rb
      route  devise_for :users
Load the created migration file. A migration file is simply a blueprint for a table. The second line of the above screen, db/migrate/created date, month, month, day, hour, minute, second, _devise_create_users.rb is the migration file.
Terminal
$ rails db:migrate
If the following screen is displayed, it is successful.
Terminal
== 20201103032107 DeviseCreateUsers: migrating ================================
-- create_table(:users)
   -> 0.0046s
-- add_index(:users, :email, {:unique=>true})
   -> 0.0014s
-- add_index(:users, :reset_password_token, {:unique=>true})
   -> 0.0014s
== 20201103032107 DeviseCreateUsers: migrated (0.0076s) =======================
At this point, devise installation is complete. You can access the login screen at the following URL. (You can check the URL of each screen with the rails routes command)
/users/sign_in

When setup is complete, routes will be automatically added to config/routes.rb and users will be included in the URL when using devise.
config/routes.rb
devise_for :users
In addition, the following description will be added to app/models/user.rb. The basic functionality of devise is described by default.
app/models/user.rb
devise :database_authenticatable, :registerable,
        :recoverable, :rememberable, :validatable
database_authenticatable Verification and encryption of passwords stored in the database
registerable User registration / edit / delete function
recoverable Password reset
rememberable Save login information. Ability to stay logged in
validatable Added email and password validation
Run the following command to create a view.
Terminal
$ rails g devise:views
If the following screen is displayed, it is successful.
Terminal
    invoke  Devise::Generators::SharedViewsGenerator
    create    app/views/devise/shared
    create    app/views/devise/shared/_error_messages.html.erb
    create    app/views/devise/shared/_links.html.erb
    invoke  form_for
    create    app/views/devise/confirmations
    create    app/views/devise/confirmations/new.html.erb
    create    app/views/devise/passwords
    create    app/views/devise/passwords/edit.html.erb
    create    app/views/devise/passwords/new.html.erb
    create    app/views/devise/registrations
    create    app/views/devise/registrations/edit.html.erb
    create    app/views/devise/registrations/new.html.erb
    create    app/views/devise/sessions
    create    app/views/devise/sessions/new.html.erb
    create    app/views/devise/unlocks
    create    app/views/devise/unlocks/new.html.erb
    invoke  erb
    create    app/views/devise/mailer
    create    app/views/devise/mailer/confirmation_instructions.html.erb
    create    app/views/devise/mailer/email_changed.html.erb
    create    app/views/devise/mailer/password_change.html.erb
    create    app/views/devise/mailer/reset_password_instructions.html.erb
    create    app/views/devise/mailer/unlock_instructions.html.erb
For example, the view on the login screen is app/views/devise/sessions/new.html.erb
The view on the sign-up screen is app/views/devise/registrations/new.html.erb.
You can edit these files to change the design.
This article was written with reference to the following information.
[* Rails *] How to use devise (rails5 version) Complete introduction to rails devise! What can devise do after all? [Rails] Let's master how to use devise and implement login authentication function!
Recommended Posts