How to implement login request processing (Rails / for beginners)

Login request processing is a process to hide a specific page from users who are not logged in. This article is an easy solution to how to implement login request processing.

Prepare a helper in $ rails g helper sessions and implement current_user and logged_in?.

app/helpers/sessions_helper.rb


module SessionsHelper
  def current_user
    @current_user ||= User.find_by(id: session[:user_id])
  end

  def logged_in?
    !!current_user
  end
end

def current_user is a method to get the currently logged in user.

@current_user ||= User.find_by(id: session[:user_id])Is If the current login user is assigned to @ current_user → Do nothing. If the current login user is not assigned to @ current_user → Get the login user fromUser.find_by (...)and assign it to @ current_user.

def logged_in? Returns true if the user is logged in, false if the user is not logged in.

Let's think about the controller.

app/controllers/application_controller.rb


class ApplicationController < ActionController::Base

  include SessionsHelper

  private

  def require_user_logged_in
    unless logged_in?
      redirect_to login_url
    end
  end
end

The method written in ApplicationController can be used in all Controllers.

Define the require_user_logged_in method. The require_user_logged_in method checks the login status, does nothing if you are logged in, and forces you back to the login page if you are not logged in. The reason I wrote include SessionsHelper is to be able to use thelogged_in?Method defined in Helper.

Now let's use the require_user_logged_in method in UsersController.

app/controllers/users_controller.rb


class UsersController < ApplicationController
  before_action :require_user_logged_in, only: [:index, :show]

  #Omitted below

For the index and show specified in before_action, the require_user_logged_in method is executed as preprocessing.

For example, when a person accesses users # index, if that person logs in, they can see users # index. However, if you are not logged in, you will be taken to the login page. In this way, you can create membership-based services. that's all.

Recommended Posts

How to implement login request processing (Rails / for beginners)
[Rails] How to implement unit tests for models
[For beginners] How to implement the delete function
[Rails] How to implement scraping
How to implement guest login in 5 minutes in rails portfolio
[Rails] How to implement star rating
[Rails] How to display error messages for comment function (for beginners)
How to implement search functionality in Rails
[RSpec on Rails] How to write test code for beginners by beginners
[Ruby] How to use slice for beginners
How to implement ranking functionality in Rails
How to implement image posting using rails
How to implement asynchronous processing in Outsystems
[For beginners] How to debug in Eclipse
How to build a Ruby on Rails environment using Docker (for Docker beginners)
(For beginners) [Rails] Time saving tech! How to install and use slim
[For Rails beginners] Summary of how to use RSpec (get an overview)
[Rails] Add page nation to table [For beginners]
[Swift] How to implement the LINE login function
How to implement a like feature in Rails
[Rails] How to easily implement numbers with pull-down
[Rails] How to use Gem'rails-i18n' for Japanese support
[For super beginners] How to use autofocus: true
How to implement Pagination in GraphQL (for ruby)
How to write Rails
How to uninstall Rails
How to make batch processing with Rails + Heroku configuration
[Rails] How to create a signed URL for CloudFront
How to implement a like feature in Ajax in Rails
[Rails, JS] How to implement asynchronous display of comments
Rails / Ruby: How to get HTML text for Mail
[Rails] Return login result in JSON format (for beginners)
How to deal with No template for interactive request
[For beginners] How to operate Stream API after Java 8
[Spring Boot] How to create a project (for beginners)
Rails learning How to implement search function using ActiveModel
How to use GitHub for super beginners (team development)
[Ruby on Rails] How to implement tagging / incremental search function for posts (without gem)
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
[rails] How to post images
[Rails] How to use enum
[Rails] How to install devise
[Rails] How to use enum
How to read rails routes
How to use rails join
(For beginners) [Rails] Install Devise
How to terminate rails server
How to write Rails validation
How to write Rails seed
[Rails] How to use validation
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to make seed
How to write Rails routing
[Rails] How to install simple_calendar
[Java] How to implement multithreading
[Rails] How to install reCAPTCHA
[Rails] How to use Scope
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
Tutorial to create a blog with Rails for beginners Part 1