質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Q&A

解決済

2回答

1685閲覧

【Rails】devise gemのconfirmableにおいてメール確認後、ログイン状態にしたい

yoangelo

総合スコア8

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

0グッド

0クリップ

投稿2020/04/17 08:41

編集2020/04/23 23:32

行ったこと

Railsアプリを作成しています。
[Rails] deviseの使い方(rails5版)
を参考に、devise gemでユーザーログイン/ログアウト機能をconfirmableモジュールを用いて以下の動作を実装しました。

  1. 新規登録画面でユーザー名、アドレス、パスワード2回、を入力する
  2. 1.で入力したアドレスにページリンク付きアカウント確認メールが送信される
  3. 2のリンクからログイン画面に転送される (新規登録完了)
  4. ログイン画面でアドレス、パスワードを入力する
  5. ログイン完了し、index画面にジャンプする

実現したいこと

確認メールのリンクをクリックすると登録完了はもちろん、自動でログイン状態になるようにしたい。(上記の動作で言うと、4.をスキップするイメージです)

こちらを実現するために必要な設定などありましたらご教示頂けますでしょうか。
また、参考になるドキュメントや記事があれば、そちらも教えていただくと幸いです。

ソースコード、開発環境など

ソースコード

ruby:

1#models/user.rb 2 3class User < ApplicationRecord 4 # Include default devise modules. Others available are: 5 # :lockable, :timeoutable, :trackable and :omniauthable 6 devise :database_authenticatable, :registerable, 7 :recoverable, :rememberable, :validatable, 8 :confirmable, :omniauthable, omniauth_providers: [:twitter] 9 10 def self.from_omniauth(auth) 11 find_or_create_by(provider: auth["provider"], uid: auth["uid"]) do |user| 12 user.provider = auth["provider"] 13 user.uid = auth["uid"] 14 user.username = auth["info"]["nickname"] 15 user.skip_confirmation! 16 end 17 end 18 19 def self.new_with_session(params, session) 20 if session["devise.user_attributes"] 21 new(session["devise.user_attributes"]) do |user| 22 user.attributes = params 23 end 24 else 25 super 26 end 27 end 28end

ruby:

1#confirmations_controller.rb 2class Users::ConfirmationsController < Devise::ConfirmationsController 3 private 4 def after_confirmation_path_for(resource_name, resource) 5 sign_in(resource) 6 reviews_path 7 end 8end

ruby:

1#application_controller.rb 2class ApplicationController < ActionController::Base 3 protect_from_forgery with: :exception 4 before_action :configure_permitted_parameters, if: :devise_controller? 5 6 def after_sign_in_path_for(resource) 7 reviews_path 8 end 9 10 protected 11 def configure_permitted_parameters 12 devise_parameter_sanitizer.permit(:sign_up, keys: [:username]) 13 devise_parameter_sanitizer.permit(:account_update, keys: [:username]) 14 end 15 16 private 17 def sign_in_required 18 redirect_to new_user_session_url unless user_signed_in? 19 end 20end 21
#認証メールのURLにアクセスした際のログ web_1 | Started GET "/users/confirmation?confirmation_token=URD69pQDCmxR9Q6Yog-a" for 172.19.0.1 at 2020-04-23 23:28:35 +0000 web_1 | Processing by Devise::ConfirmationsController#show as HTML web_1 | Parameters: {"confirmation_token"=>"URD69pQDCmxR9Q6Yog-a"} web_1 | User Load (0.9ms) SELECT `users`.* FROM `users` WHERE `users`.`confirmation_token` = 'URD69pQDCmxR9Q6Yog-a' ORDER BY `users`.`id` ASC LIMIT 1 web_1 | ↳ /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/log_subscriber.rb:98 web_1 | (0.8ms) BEGIN web_1 | ↳ /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/log_subscriber.rb:98 web_1 | User Update (7.4ms) UPDATE `users` SET `confirmed_at` = '2020-04-23 23:28:35', `updated_at` = '2020-04-23 23:28:35' WHERE `users`.`id` = 49 web_1 | ↳ /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/log_subscriber.rb:98 web_1 | (4.3ms) COMMIT web_1 | ↳ /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/log_subscriber.rb:98 web_1 | Redirected to http://localhost:3000/users/sign_in web_1 | Completed 302 Found in 66ms (ActiveRecord: 13.3ms) web_1 | web_1 | web_1 | Started GET "/users/sign_in" for 172.19.0.1 at 2020-04-23 23:28:35 +0000 web_1 | Processing by Devise::SessionsController#new as HTML web_1 | Rendering devise/sessions/new.html.erb within layouts/application web_1 | Rendered devise/shared/_links.html.erb (3.9ms) web_1 | Rendered devise/sessions/new.html.erb within layouts/application (78.4ms) web_1 | Rendered layouts/_header.html.erb (2.1ms) web_1 | Completed 200 OK in 876ms (Views: 871.2ms | ActiveRecord: 0.0ms)

必要があれば、他のコードも追記します。

開発環境

Docker ver.3
(docker-composeによる仮想環境)
Ruby:2.5.3
Rails:5.2.4

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

confirmations_controller.rb に、

private def after_confirmation_path_for(_resource_name, resource) sign_in(resource) some_where_path end

投稿2020/04/19 06:44

編集2020/04/19 06:45
winterboum

総合スコア23329

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yoangelo

2020/04/22 12:35

遅くなりすみません。ご回答ありがとうございます。 ご教示いただいた通りに、 ``` #confirmations_controller.rb .. private def after_confirmation_path_for(resource_name, resource) sign_in(resource) reviews_path end end ``` を追加してみたのですが、認証URLをアクセスすると変わらずログイン画面が表示されEメールとパスワードを要求されてしまいます…。
winterboum

2020/04/22 13:17

認証URLへのアクセスはメールが届いて最初のアクセスですか? それとも既にアクセス済のところをもう一度アクセスですか?
yoangelo

2020/04/22 23:05

認証URLへのアクセスというのは、メールが届いて最初のアクセスです。 新規登録 → メール受信 → 記載URLのアクセス
winterboum

2020/04/22 23:24

controllerが違うのかも。 認証メールのURLにアクセスした時の log を見せてください
yoangelo

2020/04/23 23:38

ログを追加しました。 自分でもログを今一度確認してみると、アクセスした後に ```Processing by Devise::ConfirmationsController#show as HTML``` とあるので、教えていただいた sign_in(resource)~ の処理はshowアクションに記載した方が良いのかな、と推測したのですが…如何でしょうか。
winterboum

2020/04/24 06:55 編集

showで認証し、成功すると after_confirmation_path_for を参照するのでこれで良いはずです。もうちょい確認してから、、 で、 after_confirmation_path_for を書いたcontroller のfile がどの dirにあるのかとclass名を教えてください
yoangelo

2020/04/26 15:52

返信が遅くなりすみません。 after_confirmation_path_forメソッドは、 app/controllers/users/confirmations_controller.rb に記載しています。 クラス名、というのは下記のことで良いでしょうか? class Users::ConfirmationsController < Devise::ConfirmationsController
winterboum

2020/04/27 00:37

合ってるなぁ、、、 showにやってみてもらえますか
yoangelo

2020/04/27 11:26

ConfirmationsControllerに ~略~  def show   sign_in(resource)   reviews_path  end ~略~  private   def after_confirmation_path_for(resource_name, resource)  end end としましたが、状況は変わらずでした。。 以下がその際のログとなります(こちらも特に変わりませんでした。) web_1 | Started GET "/users/confirmation?confirmation_token=i8mA78zoNsvdMFVWr9-d" for 172.19.0.1 at 2020-04-27 11:18:49 +0000 web_1 | Processing by Devise::ConfirmationsController#show as HTML web_1 | Parameters: {"confirmation_token"=>"i8mA78zoNsvdMFVWr9-d"} web_1 | User Load (1.3ms) SELECT `users`.* FROM `users` WHERE `users`.`confirmation_token` = 'i8mA78zoNsvdMFVWr9-d' ORDER BY `users`.`id` ASC LIMIT 1 web_1 | ↳ /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/log_subscriber.rb:98 web_1 | (0.6ms) BEGIN web_1 | ↳ /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/log_subscriber.rb:98 web_1 | User Update (3.7ms) UPDATE `users` SET `confirmed_at` = '2020-04-27 11:18:49', `updated_at` = '2020-04-27 11:18:49' WHERE `users`.`id` = 51 web_1 | ↳ /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/log_subscriber.rb:98 web_1 | (7.5ms) COMMIT web_1 | ↳ /usr/local/bundle/gems/activerecord-5.2.4.2/lib/active_record/log_subscriber.rb:98 web_1 | Redirected to http://localhost:3000/users/sign_in web_1 | Completed 302 Found in 120ms (ActiveRecord: 13.1ms) web_1 | web_1 | web_1 | Started GET "/users/sign_in" for 172.19.0.1 at 2020-04-27 11:18:49 +0000 web_1 | Processing by Devise::SessionsController#new as HTML web_1 | Rendering devise/sessions/new.html.erb within layouts/application web_1 | Rendered devise/shared/_links.html.erb (1.6ms) web_1 | Rendered devise/sessions/new.html.erb within layouts/application (88.9ms) web_1 | Rendered layouts/_header.html.erb (2.3ms) web_1 | Completed 200 OK in 1832ms (Views: 1827.1ms | ActiveRecord: 0.0ms)
winterboum

2020/04/27 13:46

はて Users::ConfirmationsController でなく、 Processing by Devise::ConfirmationsController#show が呼ばれてますね
yoangelo

2020/04/27 17:34

最初にご教示頂いた回答+ルートファイルの変更で、解決することができました。 長きに渡りご対応いただきありがとうございました。????
guest

0

winterboum 様の最初の回答(ベストアンサー)

ruby

1#users/confirmations_controller.rb 2private 3 def after_confirmation_path_for(resource_name, resource) 4 sign_in(resource) 5 reviews_path 6 end

に加えて、

ruby

1#route.rb 2.. 3 devise_for :users, controllers: { 4 confirmations: 'users/confirmations' 5 } 6..

とrouteファイルを変更することで実現することができました!
参考:
How To: Add :confirmable to Users
一次情報もしっかり確認することが大事ですね。今後の教訓とします・・・。

投稿2020/04/27 17:28

編集2020/04/27 17:31
yoangelo

総合スコア8

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問