[RUBY] [Rails 6.0] "Easy login" implementation procedure required for portfolio

Overview

When creating an application with Rails, I think that there are many people who implement the login function using devise. Among them, many people have heard that "Let's log in easily!" To the portfolio.

However, in my experience, I had a lot of trouble implementing it''. The reason is simply "because I had to do something I didn't know about devise" ``.

So in this article,

While organizing this area, I would like to output the "Easy Login Implementation Procedure".

In addition, this article is based on this wonderful article. There is also a detailed explanation on YouTube, so please have a look.

Qiita | How to implement easy login / guest login function (for portfolio)


environment


Check the implementation flow

Before we get into the implementation, let's first control the `` easy login flow''.

Easy login flow


1.Easy login button click
2-1.Any email address([email protected])If is not in the users table, add another column and create a user
2-2.Any email address([email protected])Get it if it is in the users table
3.Log in as the created / acquired user
4.Redirect to your favorite page(Mostly the top page)

In other words, all you need to do is the next step.

Required process


1.Set the controller path for easy login for routing
2.Create a controller for easy login
3.Place the link in the view

We will implement it according to this process.


Implementation: Edit 3 files and you're good to go

Then, we will implement in the order of `` routing → controller → view''.


1. Easy login routing settings

Add the following description to the routing file.

routes.rb


devise_scope :user do
  post 'users/guest_sign_in', to: 'users/guest_sessions#new_guest'
end

This will generate the following routing:

Prefix Verb URI Pattern Controller#Action
users_guest_sign_in POST /users/guest_sign_in users/guest_sessions#new_guest

Organize this content.

So let's create a controller in the specified directory and implement a simple login action.


2. Creating a controller for easy login

We will implement the new_guest action in app / controllers / users / guest_sessions_controller.rb.

Please note the above points.

app/controllers/users/guest_sessions_controller.rb


class Users::SessionsController < Devise::SessionsController
  def new_guest
    user = User.find_or_create_by!(email: "[email protected]") do |user|
      #Add required columns in block(Add nickname for yourself)
      user.nickname = "Test user"
      user.password = SecureRandom.urlsafe_base64
      # user.confirmed_at = Time.now  #Required if using Confirmable
    end
    #Login(devise method)
    sign_in user
    #Redirect to top page
    redirect_to root_path
  end  
end

find_or_create_by! Is a method like this:

By the way, this time the purpose is to show the code to be implemented, so please see the following article to supplement the namespace and password part (Qiita is an article I wrote).


3. Place a link in the view

Place a link in the view where you want to implement a simple login button. For my app, I added the following code to the top page.

top page


<%= link_to 'Guest login', users_guest_sign_in_path, method: :post %>

That's it! (The GIF below has changed the details for my app) Image from Gyazo


4. (Supplement) Partially ported to User model

It is the job of the model to interact with the DB to get and generate records. So I think it would be better to separate it as follows.

app/controllers/users/guest_sessions_controller.rb


class Users::SessionsController < Devise::SessionsController
  def new_guest
    user = User.guest
    #Login(devise method)
    sign_in user
    #Redirect to top page
    redirect_to root_path
  end  
end

app/models/user.rb


class User < ApplicationRecord

#(Omitted)

  def self.guest
    find_or_create_by!(email: "[email protected]") do |user|
      user.nickname = "Test user"
      user.password = SecureRandom.urlsafe_base64
    end
  end
end

Summary

I think there are various articles, but the basics are as shown here, so please refer to them!

Recommended Posts

[Rails 6.0] "Easy login" implementation procedure required for portfolio
Login function implementation with rails
[Apple login] Sign in with Apple implementation procedure (Ruby on Rails)
[rails] Login screen implementation in devise
[Rails] Comment function implementation procedure memo
[EC2 / Vue / Rails] EC2 deployment procedure for Vue + Rails
Rails Addition of easy and easy login function
Rails [For beginners] Implementation of comment function
Rails Basic CRUD function implementation procedure scaffold
[Rails / devise] Implementation of account information editing function / Procedure for changing redirect destination
Validation settings for Ruby on Rails login function
Use [Rails] devise Guest user function (for portfolio)
[Rails] Procedure for linking databases with Ruby On Rails
[For beginners] Procedure for creating a controller using rails
Ruby on Rails <2021> Implementation of simple login function (form_with)
How to implement login request processing (Rails / for beginners)
Implementation of Ruby on Rails login function (devise edition)
[Rails] Return login result in JSON format (for beginners)
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~