1. 問題
ログイン機能の実装のため、ログイン認証用のgemであるdevise_token_authを導入した。
前提としてDocker上でフロントをVue.js、バックエンドはRails APIで実装している。
一通りdevise_token_authの設定が終わったので、Postmanから新規登録のAPIを叩いてみる
urlは、http://localhost:8000/v1/auth
paramsは、name
, email
, password
, password_confirmation
を設定した
下記のエラーが発生した
Postman
{
"status": 500,
"error": "Internal Server Error",
"exception": "#<NoMethodError: undefined method `downcase' for nil:NilClass>",
"traces": {
"Application Trace": [],
"Framework Trace": [
{
"exception_object_id": 20620,
"id": 0,
"trace": "rack (2.2.5) lib/rack/utils.rb:464:in `[]='"
},
...
]
}
}
console
backend_1 | NoMethodError (undefined method `downcase' for nil:NilClass):
backend_1 |
backend_1 | rack (2.2.5) lib/rack/utils.rb:464:in `[]='
backend_1 | rack (2.2.5) lib/rack/utils.rb:432:in `block in initialize'
backend_1 | rack (2.2.5) lib/rack/utils.rb:432:in `each'
backend_1 | rack (2.2.5) lib/rack/utils.rb:432:in `initialize'
backend_1 | rack (2.2.5) lib/rack/utils.rb:425:in `new'
backend_1 | rack (2.2.5) lib/rack/utils.rb:425:in `[]'
backend_1 | rack (2.2.5) lib/rack/etag.rb:29:in `call'
backend_1 | rack (2.2.5) lib/rack/conditional_get.rb:40:in `call'
backend_1 | rack (2.2.5) lib/rack/head.rb:12:in `call'
2. 解決策
下記のGitHubに解決策が載っていた
https://github.com/lynndylanhurley/devise_token_auth/issues/1540
config/initializers/devise_token_auth.rbに:'authorization' => 'authorization'
を追加する
config/initializers/devise_token_auth.rb
# Makes it possible to change the headers names
config.headers_names = {:'access-token' => 'access-token',
:'client' => 'client',
:'expiry' => 'expiry',
:'uid' => 'uid',
:'token-type' => 'token-type',
# ↓ここを追加!!!
:'authorization' => 'authorization'}
修正後、再度docker-compose build
、docker-compose up
conosole
docker-compose build --no-cache
docker-compose up
もう一度APIを叩いてみたら、新規登録できた!
Postman
{
"status": "success",
"data": {
"id": 20,
"provider": "email",
"uid": "ginger-yell@example.com",
"name": "ginger-yell",
"email": "ginger-yell000@example.com",
"created_at": "2023-01-17T11:27:37.803Z",
"updated_at": "2023-01-17T11:27:37.867Z"
}
}
console
backend_1 | User Create (2.1ms) INSERT INTO `users` (`uid`, `encrypted_password`, `name`, `email`, `created_at`, `updated_at`) VALUES ('ginger-yell@example.com', '$1a$14$KhKsTVkilNgLofubgXGOdu32UsLF9W.BUCayj7w28oVeNNONDijy6', 'ginger-yell', 'ginger-yell@example.com', '2023-01-17 11:27:37.803531', '2023-01-17 11:27:37.803531')
参考