1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Deviseでログインが上手く動かなかった件

Posted at

事象

Deviseを導入してログイン機能を実装しようとしたが、上手く動かない。
まず以下の記事などを参考にしてDeviseをInstall&Settingしてみた。
https://qiita.com/Orangina1050/items/a16e655519a60f35b394

無事に設定は完了して以下の通りログイン画面が表示されるようになった。
image.png

事前にSignUp画面から登録していたユーザー情報を入力して、ログインしてみると、
指定のページに遷移はするのだが、ログイン状態になっていないという状況。

ログイン状況を正確に確認するため、ログイン後の遷移先ページ(after_sign_in_path_for)に以下のコードを埋め込む。

index.htm.erb
<% if user_signed_in? %> <!-- ユーザーがログインしているか調べる -->
  <!-- ユーザーがログインしていた時の処理 -->

  <h4> メールアドレス: <%= current_user.email %> </h4>
  <%= link_to "ログアウト", destroy_user_session_path, method: :delete %> <!-- ログアウトをする -->
<% else %>
  <!-- ユーザーがログインしていなかった時の処理 -->
  <h2> 現在ログインしていません </h2>
  <%= link_to "ログイン", new_user_session_path, class: 'post' %> <!-- ログイン画面に移行する -->
  <%= link_to "新規登録", new_user_registration_path, class: 'post' %> <!-- 新規登録画面に移行する -->
<% end %>

ログイン後に遷移先ページの表示を見るとログインできていない...

image.png

LOGを確認してみる。

Started POST "/users/sign_in" for 114.172.203.17 at 2020-08-22 18:47:49 +0900
Cannot render console from 114.172.203.17! Allowed networks: 127.0.0.0/127.255.255.255, ::1
  [1m[35m (0.3ms)[0m  [1m[35mSET NAMES utf8mb4,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483[0m
  [1m[35m (0.3ms)[0m  [1m[34mSELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC[0m
Processing by Users::SessionsController#create as HTML
  Parameters: {"authenticity_token"=>"0MTFedGB+crqsv610xukyS4r0Unf1mIuCisebD8qHfaVRwCApKj42l8dMkfdrxdfR3PoDdoBiTphkiNvn6HtWg==", "user"=>{"email"=>"test@test.co.jp", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"}
in Signin POST
  [1m[36mUser Load (0.5ms)[0m  [1m[34mSELECT `users`.* FROM `users` WHERE `users`.`email` = 'test@test.co.jp' ORDER BY `users`.`id` ASC LIMIT 1[0m
  ↳ app/controllers/users/sessions_controller.rb:15:in `create'
XX Redirect             <--確認のためのDebugメッセージ
true                   <-このタイミングでのuser_sign_in?の実行結果
Redirected to http://unicorn/maps/index.1
Completed 302 Found in 374ms (ActiveRecord: 2.8ms | Allocations: 13279)

Started GET "/maps/index.1" for 114.172.203.17 at 2020-08-22 18:47:49 +0900
Cannot render console from 114.172.203.17! Allowed networks: 127.0.0.0/127.255.255.255, ::1
  [1m[35m (0.8ms)[0m  [1m[35mSET NAMES utf8mb4,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483[0m
  [1m[35m (0.4ms)[0m  [1m[34mSELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC[0m
Processing by MapsController#index as 
  Rendering maps/index.html.erb within layouts/application
  [1m[36mAddress Load (1.2ms)[0m  [1m[34mSELECT `addresses`.* FROM `addresses`[0m
  ↳ app/views/maps/index.html.erb:45
  Rendered maps/index.html.erb within layouts/application (Duration: 39.0ms | Allocations: 7229)
  [1m[36mUser Load (0.5ms)[0m  [1m[34mSELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1[0m
  ↳ app/views/layouts/application.html.erb:20
Completed 200 OK in 1083ms (Views: 1063.1ms | ActiveRecord: 4.0ms | Allocations: 1339016)

ログイン後のリダイレクト先がなぜか”http://unicorn/maps/index.1”となっている
その後正しいURLにリダイレクトしているが...
→もしかしてこの302エラーのタイミングでログイン情報が欠落しているのでは?

解決法

以下の記事に記載のとおり、ngixの設定ファイルをちょっといじると上記最初のリダイレクト先がホスト名となりログインも上手くいった。ただ302エラーは引き続きでているのが気になるが...まー良いとしましょう。
https://qiita.com/mist_dev/items/903877646d9188ae8c6e

1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?