お世話になっております。
前提・実現したいこと
devise、devise_token_authを使用してrailsAPIでのユーザー登録機能を実装しているのですが、
バリデーションに引っかかるデータや、空のデータを送ると422 (Unprocessable Entity)がログに表示されます。
この原因が分かりません。
発生している問題・エラーメッセージ
######curlした際のレスポンス
~ ❯❯❯ curl localhost:3001/api/auth -X POST -d '{"name":"", "email":"example@example.com", "password":"password", "password_confirmation": "passwor"}' -H "content-type:application/json" {"status":"error","data":{"id":null,"provider":"email","uid":"","name":"","image":null,"email":"example@example.com","allow_password_change":false,"created_at":null,"updated_at":null}, "errors":{"password_confirmation":["doesn't match Password"],"email":["has already been taken"],"name":["can't be blank"],"full_messages":["Password confirmation doesn't match Password","Email has already been taken","Name can't be blank"]}}%
######railsのログ
Started POST "/api/auth/" for 172.23.0.1 at 2020-05-14 13:16:28 +0900 Processing by Api::Auth::RegistrationsController#create as HTML Parameters: {"name"=>"", "email"=>"", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"} (1.1ms) BEGIN User Exists? (1.7ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY '' AND `users`.`provider` = 'email' LIMIT 1 User Exists? (1.6ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = '' LIMIT 1 (3.3ms) ROLLBACK Completed 422 Unprocessable Entity in 68ms (Views: 0.7ms | ActiveRecord: 7.6ms | Allocations: 7500)
該当のソースコード
以下はブラウザの検証機能のnetworkにあった、422errorの内容です。
######General
Request URL: http://localhost:3000/api/auth/ Request Method: POST Status Code: 422 Unprocessable Entity Remote Address: 127.0.0.1:3000 Referrer Policy: no-referrer-when-downgrade
######Request Headers
Accept: application/json, text/plain, */* Accept-Encoding: gzip, deflate, br Accept-Language: ja,en-US;q=0.9,en;q=0.8 Connection: keep-alive Content-Length: 63 Content-Type: application/json;charset=UTF-8 Cookie: _ranking_app_session=lzmtqj9bPHkOBVy%2BACi3aADojSsDzd%2BnXOlYmM1KHJK7MAlH8gCWJWMh0ohOJHejWR4UqoCsyprlkboW4kJjWsulpwxfKg5pDMuZfeEOEG8lQ84LYUnWiFf5ORUT7uuUb0iRI7NIYr%2FffAd4%2BAc%3D--fP5VYg83rpGQ2SVI--vp2NeUd%2F56ei1pTrs5Di%2FA%3D%3D Host: localhost:3000 Origin: http://localhost:3000 Referer: http://localhost:3000/signup Sec-Fetch-Dest: empty Sec-Fetch-Mode: cors Sec-Fetch-Site: same-origin User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36
######Response Headers
access-control-allow-methods: GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD access-control-allow-origin: * access-control-expose-headers: access-control-max-age: 7200 cache-control: no-cache connection: close content-type: application/json; charset=utf-8 Date: Thu, 14 May 2020 04:23:16 GMT referrer-policy: strict-origin-when-cross-origin transfer-encoding: chunked vary: Origin x-content-type-options: nosniff x-download-options: noopen x-frame-options: SAMEORIGIN x-permitted-cross-domain-policies: none x-request-id: 1aa46f75-881f-423f-acb5-280eac741eff x-runtime: 0.114484 x-xss-protection: 1; mode=block
######Request Payload
{name: "", email: "", password: "", password_confirmation: ""} email: "" name: "" password: "" password_confirmation: ""
以下はrailsの情報です。
######controllers/api/auth/registrations_controller.rb
ruby
1class Api::Auth::RegistrationsController < DeviseTokenAuth::RegistrationsController 2 private 3 4 def sign_up_params 5 params.permit(:name, :email, :password, :password_confirmation) 6 end 7 8 def account_update_params 9 params.permit(:name, :email) 10 end 11end
######controllers/application_controller.rb
ruby
1class ApplicationController < ActionController::Base 2 include DeviseTokenAuth::Concerns::SetUserByToken 3 skip_before_action :verify_authenticity_token, if: :devise_controller? 4end 5
試したこと
- railsのログは、curlでもアプリの登録フォームからでも同じ表示です。(422が表示される)
- バリデーションに引っかからない正しいデータならばエラーは出ず、DBに登録できます。
補足情報(FW/ツールのバージョンなど)
ruby 2.6.3
rails (6.0.2.2)
devise (4.7.1)
devise_token_auth (1.1.3)
よろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。