taiti101112 / portfolio

0 stars 0 forks source link

質問 #122

Closed taiti101112 closed 1 week ago

taiti101112 commented 2 weeks ago

お疲れさまです。 お忙しいところすみませんがご確認のほどよろしくお願いいたします。 また、ログ以外に該当箇所が絞れておらず、必要なコードがあれば添付いたしますので教えていただければ幸いです。

エラーを解決するために調べた資料 【Rails6】deviseでログイン時、「Completed 401 Unauthorized」となり、ログインできない時の対処法(備忘録) deviseでログイン時 Completed 401 Unauthorizedエラーについて

portfolio-web-1  | Started GET "/users/sign_in" for 192.168.65.1 at 2024-09-04 12:26:29 +0900
portfolio-web-1  | Cannot render console from 192.168.65.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1
portfolio-web-1  | Processing by Users::SessionsController#new as HTML
portfolio-web-1  |   Rendering layout layouts/application.html.erb
portfolio-web-1  |   Rendering users/sessions/new.html.erb within layouts/application
portfolio-web-1  |   Rendered users/sessions/new.html.erb within layouts/application (Duration: 1.6ms | Allocations: 1162)
portfolio-web-1  |   Rendered layouts/_header.html.erb (Duration: 0.6ms | Allocations: 531)
portfolio-web-1  |   Rendered layouts/_footer.html.erb (Duration: 0.1ms | Allocations: 136)
portfolio-web-1  |   Rendered layout layouts/application.html.erb (Duration: 4.9ms | Allocations: 2573)
portfolio-web-1  | Completed 200 OK in 11ms (Views: 6.6ms | ActiveRecord: 0.0ms | Allocations: 3812)
portfolio-web-1  | 
portfolio-web-1  | 
portfolio-web-1  | Started POST "/users/sign_in" for 192.168.65.1 at 2024-09-04 12:26:40 +0900
portfolio-web-1  | Cannot render console from 192.168.65.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1
portfolio-web-1  | Processing by Users::SessionsController#create as TURBO_STREAM
portfolio-web-1  |   Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"name"=>"puma", "email"=>"puma@jp", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"ログイン"}
portfolio-web-1  |   User Load (1.8ms)  SELECT "users".* FROM "users" WHERE "users"."email" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["email", "puma@jp"], ["LIMIT", 1]]
portfolio-web-1  | Redirected to http://localhost:3000/users/8
portfolio-web-1  | Completed 303 See Other in 241ms (ActiveRecord: 1.8ms | Allocations: 2780)
portfolio-web-1  | 
portfolio-web-1  | 
portfolio-web-1  | Started GET "/users/8" for 192.168.65.1 at 2024-09-04 12:26:40 +0900
portfolio-web-1  | Cannot render console from 192.168.65.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1
portfolio-web-1  | Processing by UsersController#show as TURBO_STREAM
portfolio-web-1  |   Parameters: {"id"=>"8"}
portfolio-web-1  | Completed 401 Unauthorized in 2ms (ActiveRecord: 0.0ms | Allocations: 772)
portfolio-web-1  | 
portfolio-web-1  | 
portfolio-web-1  | Started GET "/users/sign_in" for 192.168.65.1 at 2024-09-04 12:26:40 +0900
portfolio-web-1  | Cannot render console from 192.168.65.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1
portfolio-web-1  | Processing by Users::SessionsController#new as TURBO_STREAM
portfolio-web-1  |   Rendering layout layouts/application.html.erb
portfolio-web-1  |   Rendering users/sessions/new.html.erb within layouts/application
portfolio-web-1  |   Rendered users/sessions/new.html.erb within layouts/application (Duration: 1.1ms | Allocations: 1081)
portfolio-web-1  |   Rendered layouts/_header.html.erb (Duration: 0.3ms | Allocations: 500)
portfolio-web-1  |   Rendered layouts/_footer.html.erb (Duration: 0.1ms | Allocations: 126)
portfolio-web-1  |   Rendered layout layouts/application.html.erb (Duration: 2.8ms | Allocations: 2380)
portfolio-web-1  | Completed 200 OK in 6ms (Views: 3.5ms | ActiveRecord: 0.0ms | Allocations: 3410)
portfolio-web-1  | 
portfolio-web-1  | 
kenchasonakai commented 2 weeks ago

@user == current_userがfalseになっていることが原因であり、@userの認証に関する不具合が原因と考えられます。

ログには上記のようなコードは出ていないと思いますが、関係するコードがどこなのかはわかりますか?

わからない場合はログに該当するコントローラーやviewが表示されていると思うので関係ありそうなところを探してみてみるとよいと思います

taiti101112 commented 2 weeks ago

ログ内で「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に変更したりしましたが解決することができませんでした。

これ以外にどこか着目すべき点がありましたら教えていただけますでしょうか? よろしくお願いいたします。

kenchasonakai commented 2 weeks ago

ここのことを言っていますかね? もしそうであればデバッグツールを使って@userとcurrent_userの中身を確認して意図通りのものが入っているか見る感じかと思います

  def correct_user
    redirect_to(root_path) unless @user == current_user
  end