LoginSignup
3
0

More than 1 year has passed since last update.

devise_token_authで["Missing 'confirm_success_url' parameter."]のエラーが出た話

Posted at

はじめに

現在、rails api + react(typescript)でポートフォリオを開発しています。
こちらの記事がとてもわかりやすく参考にさせて頂いているのですが、curlコマンドでアカウント作成とログインができるかを確認する際にエラーが起きてしまいました。
英語の記事を翻訳して解決することができたのですが、同じエラーで詰まっている人もいるかもしれないのでqiitaに投稿しておこうと思いました。

"Missing 'confirm_success_url' parameter."が発生

参考にしている記事の手順通りに進めると、本来は下記のcurlコマンドで記載したカラムの内容に応じてアカウントを作成とログインを行えるはずなのですが、コマンドを叩くと"Missing 'confirm_success_url' parameter."のエラーが返されます。

#アカウント作成コマンド
$ curl -X POST http://localhost:3001/api/v1/auth -d '[user_name]=test&[email]=test@example.com&[password]=password&[password_confirmation]=password'

#エラーが返ってくる
{"success":false,"errors":["Missing 'confirm_success_url' parameter."],"status":"error","data"{
"id":null,"provider":"email","uid":"","allow_password_change":false,"user_name":test,"nickname":null,"user_self_introduction":null,"image":null,"email":"test@example.com","created_at":null,"updated_at":null}}
#ログインコマンド
$ curl -X POST -v http://localhost:3001/api/v1/auth/sign_in -d '[email]=test@example.com&[password]=password'

#HTTP/1.1 401 Unauthorizedでエラーが返ってきている(ログインができていない)
Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 127.0.0.1:3001...
* Connected to localhost (127.0.0.1) port 3001 (#0)
> POST /api/v1/auth/sign_in HTTP/1.1
> Host: localhost:3001
> User-Agent: curl/7.79.1
> Accept: */*
> Content-Length: 44
> Content-Type: application/x-www-form-urlencoded
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 401 Unauthorized
< X-Frame-Options: SAMEORIGIN
< X-XSS-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< X-Download-Options: noopen
< X-Permitted-Cross-Domain-Policies: none
< Referrer-Policy: strict-origin-when-cross-origin
< Content-Type: application/json; charset=utf-8
< Vary: Accept, Origin
< Cache-Control: no-cache
< X-Request-Id: cfbee3c8-99a8-46e7-b2bf-783d8818c4ae
< X-Runtime: 0.215987
< Transfer-Encoding: chunked
< 
* Connection #0 to host localhost left intact

confirm_success_urlとは?

サインアップ時(アカウント作成時)に登録したアドレスに認証メールが送られます。
認証成功をしたときに推移するurlが、confirm_success_urlです。

単純に"Missing 'confirm_success_url' parameter"を翻訳すると、"「confirm_success_url」パラメーターがありません"とあるので、今度は"confirm_success_url"を記載してアカウント作成を試みます。(リダイレクトを試みるだけなので、僕のqiitaのページを指定しました。)

curl -X POST http://localhost:3001/api/v1/auth -d '[name]=test&[email]=test@example.com&[password]=password&[password_confirmation]=password&[confirm_success_url]=https://qiita.com/shuhei_m’

ですが、変わらずエラーを返してしまいます。

{"status":500,"error":"Internal Server Error",
"exception":"#\u003c ActionView::Template::Error: undefined method `confirmation_url'
 ~~以降長文なので省略

"Missing 'confirm_success_url' parameter."で検索すると原因が判明

"Missing 'confirm_success_url' parameter."で検索をかけると英語のサイトで翻訳をかけると解決をすることができました。

原因は、"include DeviseTokenAuth::Concerns::User"を記述する位置でした。
"devise"の記述よりも上に"include DeviseTokenAuth::Concerns::User"が上にあるとエラーが起きてしまうとのことでした。

#エラーが起きた際の記述
class User < ActiveRecord::Base
  include DeviseTokenAuth::Concerns::User
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise  :database_authenticatable, :registerable,
          :recoverable, :rememberable, :trackable, :validatable
end
#エラーが起きない記述
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise  :database_authenticatable, :registerable,
          :recoverable, :rememberable, :trackable, :validatable
  include DeviseTokenAuth::Concerns::User
end

実際に位置を変更したところ、問題なくアカウントの作成・ログインが成功しました!

#アカウント作成
curl -X POST http://localhost:3001/api/v1/auth -d '[name]=test&[email]=test@example.com&[password]=password&[password_confirmation]=password' 

#作成成功!
{"status":"success","data":{"id":1,"provider":"email","uid":"test@example.com","allow_password_change":false,"user_name":null,"nickname":null,"user_self_introduction":null,"image":null,"email":"test@example.com","created_at":"2022-08-13T15:05:21.134Z","updated_at":"2022-08-13T15:05:21.207Z"}
#ログイン実行
curl -X POST -v http://localhost:3001/api/v1/auth/sign_in -d '[email]=test@example.com&[password]=password'

#ログイン成功!
Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 127.0.0.1:3001...
* Connected to localhost (127.0.0.1) port 3001 (#0)
> POST /api/v1/auth/sign_in HTTP/1.1
> Host: localhost:3001
> User-Agent: curl/7.79.1
> Accept: */*
> Content-Length: 44
> Content-Type: application/x-www-form-urlencoded
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< X-Frame-Options: SAMEORIGIN
< X-XSS-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< X-Download-Options: noopen
< X-Permitted-Cross-Domain-Policies: none
< Referrer-Policy: strict-origin-when-cross-origin
< Content-Type: application/json; charset=utf-8
< Vary: Accept, Origin
< access-token: *************
< token-type: Bearer
< client: *************
< expiry: 1661612817
< uid: test@example.com
< ETag: W/"ef8d38b1eaad744696c894d0f386ed67"
< Cache-Control: max-age=0, private, must-revalidate
< X-Request-Id: 4286093e-9538-42e1-a95d-9acbb4550c4d
< X-Runtime: 0.397582
< Transfer-Encoding: chunked
< 
* Connection #0 to host localhost left intact
{"data":{"email":"test@example.com","uid":"test@example.com","id":1,"provider":"email","allow_password_change":false,"user_name":null,"nickname":null,"user_self_introduction":null,"image":null}

今回のエラーは解決できましたが、まだまだdevise/devise_token_authへの理解をもっと深めないといけないな、と強く感じるきっかけになりました。
公式ドキュメントから理解を深めていく癖を頑張ってつけていきます、、!

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