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

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

新規登録して質問してみよう
ただいま回答率
85.50%
Devise

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

Ruby on Rails

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

Q&A

解決済

2回答

918閲覧

deviseで認証周りを実装してもメールが飛ばない

ka-bi-

総合スコア17

Devise

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

Ruby on Rails

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

0グッド

0クリップ

投稿2020/06/29 12:46

前提・実現したいこと

devise と devise_token_auth のgemを共存させたところ、管理画面から登録した時のみメールが飛ばない

【参考サイト】
[Rails5] devise_token_auth でAPIを作成する / 新規登録・ログイン
https://tackeyy.com/blog/posts/token-base-api-with-rails-and-devise-token-auth

devise_token_auth導入時、ユーザ登録確認メールが送られない事象の対応方法
https://qiita.com/ruby_kumagoro/items/e010ae5099d62335a3d6
→このサイトを参考にして追記したところ、管理画面からの登録でメールを送信することができました、APIでの登録でメールが2件送信されてしまいました。

【サーバーの前提条件】
・アカウントの管理画面はRailsのviewで作成。
・管理画面はgem『devise』をそのまま利用。
・アカウント作成時にURL認証付きのメールを送り、アドレスの存在チェックを行う

【使用gem】
・devise
・devise_token_auth

発生している問題・エラーメッセージ

参考サイトどおりに組んだところ、
APIにてユーザーを作成した時はURL認証付きのメールが送られ正しく処理されるが
Viewで作成した管理画面からユーザーを作成した時はURL認証付きのメールが送られない、
といった症状が発生している。

実現したい仕様

ユーザーを登録する時は必ずURL認証付きのメールを送るようにしたい。

コード

※ViewはDeviseのデフォルト画面をそのまま利用しております。
※ソース詳細は上に記載しました参考サイトをご参照ください。

[app/controllers/application_controller.rb]

Ruby

1class ApplicationController < ActionController::Base 2 protect_from_forgery with: :exception 3 before_action :set_host 4 5 def set_host 6 Rails.application.routes.default_url_options[:host] = request.host_with_port 7 end 8end

[app/controllers/api/v1/base_controller.rb]
※Devise専用親コントローラー

Ruby

1class Api::V1::BaseController < ActionController::Base 2 include DeviseTokenAuth::Concerns::SetUserByToken 3 protect_from_forgery with: :null_session # トークン認証のためCSRFは使わない 4end

[app/controllers/api/v1/auth/registrations_controller.rb]
※API利用時のDeviseコントローラー

Ruby

1class Api::V1::Auth::RegistrationsController < DeviseTokenAuth::RegistrationsController 2 before_action :configure_permitted_parameters, if: :devise_controller? 3 4 protected 5 6 def configure_permitted_parameters 7 devise_parameter_sanitizer.permit(:sign_up, keys: [:name]) 8 end 9 end

[app/models/user.rb]

Ruby

1class User < ActiveRecord::Base 2 devise :database_authenticatable, :registerable, 3 :recoverable, :rememberable, :validatable, 4 :confirmable 5 include DeviseTokenAuth::Concerns::User 6end

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

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

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

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

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

guest

回答2

0

ベストアンサー

私も同じところでつまづきました。根本的な問題はわからなかったですが、あなたが参照しているサイト
https://qiita.com/ruby_kumagoro/items/e010ae5099d62335a3d6
とdeviseのgithubのソースコード
https://github.com/heartcombo/devise
を参考に自分なりに解決できたので記載しておきます。
devise用のcontorllerを作成して(これは調べるとすぐ出てきます)registrations_controller.rbで以下のようにファイルを編集します。

registrations_controller.rb

1# frozen_string_literal: true 2 3class Users::RegistrationsController < Devise::RegistrationsController 4 # before_action :configure_sign_up_params, only: [:create] 5 # before_action :configure_account_update_params, only: [:update] 6 7 # GET /resource/sign_up 8 # def new 9 # super 10 # end 11 12 # POST /resource 13 def create 14 build_resource(sign_up_params) 15 16 resource.save 17 yield resource if block_given? 18 if resource.persisted? 19 if resource.active_for_authentication? 20 set_flash_message! :notice, :signed_up 21 sign_up(resource_name, resource) 22 respond_with resource, location: after_sign_up_path_for(resource) 23 else 24 set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}" 25 expire_data_after_sign_in! 26 resource.send_confirmation_instructions 27 respond_with resource, location: after_inactive_sign_up_path_for(resource) 28 end 29 else 30 clean_up_passwords resource 31 set_minimum_password_length 32 respond_with resource 33 end 34 end 35 36 # GET /resource/edit 37 # def edit 38 # super 39 # end 40 41 # PUT /resource 42 # def update 43 # super 44 # end 45 46 # DELETE /resource 47 # def destroy 48 # super 49 # end 50 51 # GET /resource/cancel 52 # Forces the session data which is usually expired after sign 53 # in to be expired now. This is useful if the user wants to 54 # cancel oauth signing in/up in the middle of the process, 55 # removing all OAuth session data. 56 # def cancel 57 # super 58 # end 59 60 # protected 61 62 # If you have extra params to permit, append them to the sanitizer. 63 # def configure_sign_up_params 64 # devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute]) 65 # end 66 67 # If you have extra params to permit, append them to the sanitizer. 68 # def configure_account_update_params 69 # devise_parameter_sanitizer.permit(:account_update, keys: [:attribute]) 70 # end 71 72 # The path used after sign up. 73 # def after_sign_up_path_for(resource) 74 # super(resource) 75 # end 76 77 # The path used after sign up for inactive accounts. 78 # def after_inactive_sign_up_path_for(resource) 79 # super(resource) 80 # end 81end 82

上記のようにすると、webからでもメールが送信され、apiからでもメールが一通だけ送信されます。
やったことの内容としては、deviseのソースコードからregistration#create時にどのような処理か読み取って、無理矢理メール送信の処理を適切な位置に追加した感じです。

投稿2021/03/09 03:00

Nagai-S

総合スコア7

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

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

0

def configure_permitted_parameters devise_parameter_sanitizer.permit(:sign_up, keys: [:name]) end

これがAPIを通るコントローラーにしか書かれてないからです
参考にしたサイトはAPIで「のみ」作成できるように改変するサイトなのではないですか?

ふつうの devise の設定と同じく
ApplicationController に

before_action :configure_permitted_parameters, if: :devise_controller? def configure_permitted_parameters devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname]) end

とかかけばいいと思います

(ほかにも原因はあるかもしれないですが少なくともこれをしないとストロングパラメータが通りません)

投稿2020/07/02 08:55

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

ka-bi-

2020/07/02 10:01

ご回答ありがとうございます。 おっしゃるとおりでしたので、修正してみましたが 管理画面からの登録ではメールが飛びませんでした。 他に原因がありそうです。 > 参考にしたサイトはAPIで「のみ」作成できるように改変するサイト ではないハズなのですが、参考サイトではメール認証までは行っておりませんでした。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問