行ったこと
Railsアプリを作成しています。
[Rails] deviseの使い方(rails5版)
を参考に、devise gem
でユーザーログイン/ログアウト機能をconfirmable
モジュールを用いて以下の動作を実装しました。
- 新規登録画面でユーザー名、アドレス、パスワード2回、を入力する
- 1.で入力したアドレスにページリンク付きアカウント確認メールが送信される
- 2のリンクからログイン画面に転送される (新規登録完了)
- ログイン画面でアドレス、パスワードを入力する
- ログイン完了し、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
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/04/22 12:35
2020/04/22 13:17
2020/04/22 23:05
2020/04/22 23:24
2020/04/23 23:38
2020/04/24 06:55 編集
2020/04/26 15:52
2020/04/27 00:37
2020/04/27 11:26
2020/04/27 13:46
2020/04/27 17:34