Hello! In this first post, I'll show you how to implement the login feature using devise. I would like to explain the flow from installation to actual display and login.
What is devise in the first place? devise is a gem that can easily implement login function, user registration function, etc. In the initial state, you can log in and sign up by setting ** email and password **. It is a gem that you definitely want to use when you want to implement the membership function!
First, let's install devise.
Write "gem'devise'" at the bottom of the Gemfile.
Gemfile
gem 'devise'
Let the application load the added gem. Type the following in the terminal.
$ bundle install
Then make the initial settings for devise.
$ rails g devise:install
Installation is successful if the following message is displayed!
Now let's actually display the login screen. This time, we will create a user table in the database to store user information. The table name is "User". Type the following into your terminal:
$ rails g devise User
Originally, if you want to create a table from scratch, you need to create a column along with the table, but if you use devise, ** user table and column will be created automatically **. Let's check the migration file that was actually created.
db/migrate/(Year, month, day, hour, minute, second)_devise_create_users.rb
class DeviseCreateUsers < ActiveRecord::Migration[5.2]
def change
create_table :users do |t|
## Database authenticatable
t.string :email, null: false, default: "" #mail address
t.string :encrypted_password, null: false, default: "" #password
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
## Trackable
# t.integer :sign_in_count, default: 0, null: false
# t.datetime :current_sign_in_at
# t.datetime :last_sign_in_at
# t.string :current_sign_in_ip
# t.string :last_sign_in_ip
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
t.timestamps null: false
end
add_index :users, :email, unique: true
add_index :users, :reset_password_token, unique: true
# add_index :users, :confirmation_token, unique: true
# add_index :users, :unlock_token, unique: true
end
end
The columns required for the login function are an email address and password. The email address is on the line t.string: email, ……, and the password is on the line t.string: encrypted_password, …….
Once confirmed, migrate this file to the database.
$ rails db:migrate
If you do so far, you are ready! Finally, let's display it in the browser.
Start the server with rails s and enter ** "/ users / sign_in" ** after the URL to open the login screen. It should look like this. Then enter ** "/ users / sign_up" ** after the URL to open the sign-up screen.
If this display is displayed, it is complete. After that, let's rewrite as you like and create an original site! Until the end Thank you for reading! I would like to write more detailed articles and advanced contents in the future, so I would be grateful if you could read it. I also do Twitter. We also send out daily tweets and information on rare occasions, so please follow us ☺️ → @saketi_sei
Recommended Posts