LoginSignup
1
1

More than 3 years have passed since last update.

簡単ログイン機能実装のエラー原因はauthenticate_user!だった

Last updated at Posted at 2020-07-01

はじめに

こちらの記事を参考に実装しましたので、コードで不明な点はこちらの記事を見にいってください。
ゲストログイン・簡単ログイン機能の実装方法(ポートフォリオ用)

発生している問題・エラーメッセージ

簡単ログイン機能の実装で、ログイン画面に「簡単ログイン」ボタンを設置したが、ボタン押してもログインできない。

ターミナル
Processing by HomesController#new_guest as HTML
Parameters:{"authenticity_token"=>"c1Qc02T4i6+77OtxhGDwxJwEQUYO8d9cIncoNjZ/hXa5c3IzxHPjFAr2QleTBCuMpnxd5+1Sk+HRa9RNXLGSkg=="}
Completed 401 Unauthorized in 1ms (ActiveRecord: 0.0ms)

ソースコード

routes.rb
post '/homes/guest_sign_in', to: 'homes#new_guest'
homes_controller.rb
class HomesController < ApplicationController

  def new_guest
    user = User.find_or_create_by(email: 'guest@example.com') do |user|
      user.password = SecureRandom.urlsafe_base64
    end
    sign_in user
    redirect_to root_path, notice: 'ゲストユーザーとしてログインしました。'
  end
end
devise/sessions/new.html.haml
(省略)
.easyLogin
  = link_to 'かんたんログイン', homes_guest_sign_in_path, method: :post, class:"btn btn-lg btn-success center-block"

考えたこと

  • sessionで保持しているものとauthenticity_tokenをキーとして送られたコードが異なる?
  • authenticity_tokenはログイン画面のフォームのinputタグの中にあったので、フォーム外に簡単ログインボタンを設置すれば、authenticity_tokenが関係なくなり解決される?

→結果的にどちらも間違っていた

うまくいった時のソースコード

ターミナル
Processing by Users::SessionsController#new_guest as HTML
Parameters: {"authenticity_token"=>"2UEwYr4gIvtHF4GqHNPaXIiWgQSbEXPVjI9kneVS27oTZl6CHqtKQPYNKIwLtwEUsu6dpXiyP2h/k5jmj5zMXg=="}
  User Load (4.0ms)  SELECT  `users`.* FROM `users` WHERE `users`.`email` = 'guest@example.com' LIMIT 1
  ↳ app/models/user.rb:31
Redirected to http://localhost:3000/
Completed 302 Found in 10ms (ActiveRecord: 4.0ms)
routes.rb
devise_scope :user do
  post 'users/guest_sign_in', to: 'users/sessions#new_guest'
end
user.rb
(省略)
def self.guest
  find_or_create_by!(email: 'guest@example.com') do |user|
    user.password = SecureRandom.urlsafe_base64
  end
end
sessions_controller.rb
class Users::SessionsController < Devise::SessionsController
  def new_guest
    user = User.guest
    sign_in user
    redirect_to root_path, notice: 'ゲストユーザーとしてログインしました。'
  end
end
devise/sessions/new.html.haml
(省略)
.easyLogin
  = link_to 'かんたんログイン', users_guest_sign_in_path, method: :post, class:"btn btn-lg btn-success center-block"

エラー原因

homes_controller.rb
class HomesController < ApplicationController
(省略)
end

homes_controllerapplicatoin_controllerを継承していました。

applicatoin_controller.rb
(省略)
before_action :authenticate_user!

protected

def configure_permitted_parameters
  devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
end

before_action :authenticate_user!は、ログイン済みのユーザーだけを許可するので、これが原因で弾かれていました。

before_action :authenticate_user!をコメントアウトしたら、homes_controllerでも問題なく画面遷移しました。

sessions_controllerの方が自然なので、実装はhomes_controllerではなくsessions_controllerで設定しました。

参考記事

ゲストログイン・簡単ログイン機能の実装方法(ポートフォリオ用)

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1