[RUBY] Implement login function in Rails simply by name and password (2)

Continuation of Last time. From here, we will implement the login screen and login process.

procedure

--Create a Session controller

MacBook % rails g controller Sessions new

--Edit the routes file as below

config/routes


Rails.application.routes.draw do
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
~

--Next, create a login form. This time it is a direct writing without using Bootstrap etc.

app/views/sessions/new.html.erb


<body>
  <h1>Login</h1>
  <%= form_for(:session, url: login_path) do |f| %>
    <div class="input-name">
      <%= f.label :password, 'Room name' %>
      <%= f.text_field :name, placeholder: "Please enter the name of the room" %>
    </div>
    <div class="form-group">
      <%= f.label :password, 'password' %>
      <%= f.password_field :password, placeholder: 'Please enter your password' %>
      <%= f.submit 'Login' %>
    </div>
  <% end %>
</body>

--Create a form and access http: // localhost: 3000 / login to check --Once confirmed, then add create and destroy actions to the controller actions

app/controllers/sessions_controller.rb


class SessionsController < ApplicationController
  def new
  end

  def create
    room = Room.find_by(name: params[:session][:name])
    if room && room.authenticate(params[:session][:password])
    else
    end
  end

  def destroy
  end
end

--Write the process when login fails in the create action. (Bootstrap etc. are not used)

app/controllers/sessions_controller.rb


~
  def create
    room = Room.find_by(name: params[:session][:name])
    if room && room.authenticate(params[:session][:password])
    else
      flash[:alert] = 'The name or password is wrong'
      redirect_to '/login'
    end
  end
~

--Added session helper that automatically expires when you close the browser

app/controllers/application_controller.rb


class ApplicationController < ActionController::Base
  include SessionsHelper
end

--Define helper method and log_in method

app/helpers/sessions_helper.rb


module SessionsHelper
  #Store encrypted roomID in temporary cookies in the browser
  def log_in(room)
    session[:room_id] = room.id
  end
end

--Now you can log in, so define the create action

/app/controllers/sessions_controller.rb


~
  def create
    room = Room.find_by(name: params[:session][:name])
    if room && room.authenticate(params[:session][:password])
      log_in room
      redirect_to receptions_path
    else
      flash[:alert] = 'The name or password is wrong'
      redirect_to '/login'
    end
  end
~
You can now log in from your browser screen.

Continued [Implementing login function in Rails simply with name and password (3)]

(https://qiita.com/yongjugithub/items/dbb880f085f99cc9af93)

Recommended Posts

Implement login function in Rails simply by name and password (1)
Implement login function in Rails simply by name and password (2)
Implement login function simply with name and password in Rails (3)
Implement simple login function in Rails
Implement application function in Rails
Implement follow function in Rails
How to implement authentication process by specifying user name and password in Spring Boot
[Ruby on Rails] Implement login function by add_token_to_users with API
Implement CSV download function in Rails
Implement user registration function and corporate registration function separately in Rails devise
[Rails] Function restrictions in devise (login / logout)
Rails Addition of easy and easy login function
Posting function implemented by asynchronous communication in Rails
Implement star rating function using Raty in Rails6
Implement iteration in View by rendering collection [Rails]
[Rails] Implement search function
Implement markdown in Rails
How to implement guest login in 5 minutes in rails portfolio
Implement post search function in Rails application (where method)
[Rails] Implement credit card registration / deletion function in PAY.JP
Implement user follow function in Rails (I use Ajax) ②
Implement user follow function in Rails (I use Ajax) ①
Try to implement tagging function using rails and js
Implement LTI authentication in Rails
Login function implementation with rails
Implement import process in Rails
With Spring boot, password is hashed and member registration & Spring security is used to implement login function.
[Rails] Implement image posting function
[Rails] I tried to implement "Like function" using rails and js
[Rails] A simple way to implement a self-introduction function in your profile
I tried to implement Ajax processing of like function in Rails
Implement star evaluation function in Rails 6 (Webpacker is installed as standard)
Add a search function in Rails.
Enable jQuery and Bootstrap in Rails 6 (Rails 6)
[rails] Login screen implementation in devise
Implement tagging function in form object
Implement PHP implode function in Java
Implement a contact form in Rails
Remove "assets" and "turbolinks" in "Rails6".
CRUD features and MVC in Rails
[Ruby on Rails] How to log in with only your name and password using the gem devise
How to implement a slideshow using slick in Rails (one by one & multiple by one)
[Rails] How to log in with a name by adding a devise name column
"Teacher, I want to implement a login function in Spring" ① Hello World
How to implement search functionality in Rails
How to change app name in rails
Try to implement login function with Spring-Boot
Make a login function with Rails anyway
Implemented follow function in Rails (Ajax communication)
How to implement ranking functionality in Rails
Implement button transitions using link_to in Rails
Login function implementation by Spring Security (securityConfig)
A memo to simply create a form using only HTML and CSS in Rails 6
How to implement image posting function using Active Storage in Ruby on Rails
[Rails] Give this article to you who looked up in "devise name login"
Parse the date and time string formatted by the C asctime function in Java