Addition of guest login function

Introduction

This is a memorandum when the ** guest login function ** was added so that you can log in with the touch of a button to make it easier to see the contents when creating the original application. ** devise ** is used for new registration and login functions.

guest user implementation

models/users.rb


  def self.guest
    find_or_create_by!(username: 'The guests', email: '[email protected]') do |user|
      user.password = SecureRandom.urlsafe_base64
    end
  end

First, set the routing.

routes.rb


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

new_guest, add the action to ʻusers / sessions_controller.rb. ʻAdd the users directory to controllers.

users/sessions_controller.rb


class Users::SessionsController < Devise::SessionsController
  def new_guest
    user = User.guest
    sign_in user
    redirect_to root_path, notice: 'You have logged in as a guest user.'
  end
end

Finally, add a view such as a button to complete the implementation.

application.html.erm


<p class="control">
  <%= link_to 'Guest login', users_guest_sign_in_path, class: "button is-warning is-fullwidth", method: :post %>
</p>

Finally

Addition of a very important function to let you see the portfolio (original app). It was relatively easy to implement. Thank you for reading to the end: grin:

Recommended Posts

Addition of guest login function
Rails Addition of easy and easy login function
Login function
[Rails] Addition of Ruby On Rails comment function
Implementation of Ruby on Rails login function (Session)
Story of implementing login function using gem sorcery
[Note] Summary of rails login function using devise ①
[Algorithm] Addition of digits
Implementation of search function
Implementation of pagination function
Ruby on Rails <2021> Implementation of simple login function (form_with)
Implementation of Ruby on Rails login function (devise edition)
Rails application guest login function implemented (devise not used)
Rails tutorial (6th edition) Background operation of login function
Implementation of sequential search function
Implementation of like function (Ajax)
[Rails 6] Implementation of search function
Implementation of image preview function
[Rails] Implementation of category function
Login function with Spring Security
Implementation of category pull-down function
Login function implementation with rails
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
[Rails] Implementation of CSV import function
[Rails] Asynchronous implementation of like function
[Rails] Implementation of image preview function
About error handling of comment function
Kaminari --Added pagination function of Rails
[Rails] About implementation of like function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
Implement simple login function in Rails
Implementation of like function in Java