Closed taiti101112 closed 1 week ago
@user == current_user
がfalseになっていることが原因であり、@user
の認証に関する不具合が原因と考えられます。
ログには上記のようなコードは出ていないと思いますが、関係するコードがどこなのかはわかりますか?
わからない場合はログに該当するコントローラーやviewが表示されていると思うので関係ありそうなところを探してみてみるとよいと思います
ログ内で「Started GET "/users/8" 」と出ていることからログインは成功していると思いました。 そしてこのログの直後に「Completed 401 Unauthorized」が発生しているため、認証(@user == current_user)に失敗していると推測しました(showアクションには@user==current_userがtrueでないと遷移できない)
解決策としてはconfig/initializers/devise.rbの認証キー設定(config.authentication_keys)をemailからnameとemailに変更したり、逆にログインフォームの入力項目をname、email、passwordからemail、passwordに変更したりしましたが解決することができませんでした。
これ以外にどこか着目すべき点がありましたら教えていただけますでしょうか? よろしくお願いいたします。
ここのことを言っていますかね?
もしそうであればデバッグツールを使って@user
とcurrent_userの中身を確認して意図通りのものが入っているか見る感じかと思います
def correct_user
redirect_to(root_path) unless @user == current_user
end
お疲れさまです。 お忙しいところすみませんがご確認のほどよろしくお願いいたします。 また、ログ以外に該当箇所が絞れておらず、必要なコードがあれば添付いたしますので教えていただければ幸いです。
実装したいもの・解決したいもの ログインすると「You need to sign in or sign up before continuing.」と表示され、ログインすることができない問題を解決したい。
エラー内容 ログインすると「You need to sign in or sign up before continuing.」と表示され、ログインできません。 ログを確認すると、ログイン自体は成功していますが、@userとcurrent_userが一致しておらずリダイレクトが発生し、「401 Unauthorized」となっています。 また、@user == current_userがtrueにならないため、アプリ上ではログインできていない状態となっています。
エラーの意味とエラー内容から推測される原因 @user == current_userがfalseになっていることが原因であり、@userの認証に関する不具合が原因と考えられます。 コードを過去の状態に戻してもエラーが解消しないため、マイグレーション関連の問題が関係しているかと思います。
エラーを解決するために調べた資料 【Rails6】deviseでログイン時、「Completed 401 Unauthorized」となり、ログインできない時の対処法(備忘録) deviseでログイン時 Completed 401 Unauthorizedエラーについて