Gokul595 / api_guard

JWT authentication solution for Rails APIs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

422 error, Unprocessable Entity, InvalidAuthenticityToken when trying to sign_up

powerfulsheron opened this issue · comments

Hi Gokul,

I followed your tutorial and tried to integrate api_guard on my rails api on an existing User model.

Everything goes well regarding the installation but when trying to create my first user with postman :

POST http://localhost:3000/users/sign_up
{
"email": "lorenzo.canavaggio@laposte.net",
"password": "test",
"password_confirmation": "test",
"birthdate": "Mar, 28 Mar 1991",
"description": "Web Developer",
"profilePictureUrl": "https://www.docker.com/sites/default/files/social/docker_facebook_share.png",
"inscriptionDate": "Mar, 28 Mar 1991",
"firstname": "Canavaggio",
"lastname": "Lorenzo"
}

I get this error :

{
    "status": 422,
    "error": "Unprocessable Entity",
    "exception": "#<ActionController::InvalidAuthenticityToken: ActionController::InvalidAuthenticityToken>",
    "traces": {
        "Application Trace": [],
        "Framework Trace": [
            {
                "id": 0,
                "trace": "actionpack (5.2.3) lib/action_controller/metal/request_forgery_protection.rb:211:in `handle_unverified_request'"
            },
...

So I guess the endpoint is correctly mapped but I may have missed something nor i'm not experienced enough to understand the issue. Could you help me out ?

@powerfulsheron From the error log I can see it is expecting authenticity token in the request. Let me check and update you.

Thanks ! Don't hesitate if you need more info to reproduce the behaviour :)

@powerfulsheron Thanks for reporting the issue.

This has been fixed in v0.2.1. Please update and check. And, feel free to reopen this issue if it doesn't work.

Hi @Gokul595 !
Thank you so much, I will test later today and let you know :)

Hi @Gokul595,

So I updated the gem and the error was gone. But it was not working yet : when trying to register a user with correct body parameters I had an error like Can't convert nil to String Value on the password field.
What I did to make it work after a bit of research was to generate the api_guard initializer and create secret keys with this bundle : https://github.com/sinsoku/rails-env-credentials
I don't know if this is the right thing to do, you will understand the behaviour better than me.
Again don't hesitate if you need more info

@powerfulsheron Can you please post the error log? It will be useful to find the cause.

Hi @Gokul595 , sorry for the delay, I had to rollback my project to get the log back.
Here is the error log :

    "status": 500,
    "error": "Internal Server Error",
    "exception": "#<TypeError: no implicit conversion of nil into String>",
    "traces": {
        "Application Trace": [],
        "Framework Trace": [
            {
                "id": 0,
                "trace": "jwt (2.2.1) lib/jwt/algos/hmac.rb:14:in `digest'"
            },
            {
                "id": 1,
                "trace": "jwt (2.2.1) lib/jwt/algos/hmac.rb:14:in `sign'"
            },
            {
                "id": 2,
                "trace": "jwt (2.2.1) lib/jwt/signature.rb:37:in `sign'"
            },
            {
                "id": 3,
                "trace": "jwt (2.2.1) lib/jwt/encode.rb:57:in `encode_signature'"
            },
            {
                "id": 4,
                "trace": "jwt (2.2.1) lib/jwt/encode.rb:34:in `encoded_signature'"
            },
            {
                "id": 5,
                "trace": "jwt (2.2.1) lib/jwt/encode.rb:20:in `segments'"
            },
            {
                "id": 6,
                "trace": "jwt (2.2.1) lib/jwt.rb:24:in `encode'"
            },
            {
                "id": 7,
                "trace": "api_guard (0.2.1) lib/api_guard/jwt_auth/json_web_token.rb:21:in `encode'"
            },
            {
                "id": 8,
                "trace": "api_guard (0.2.1) lib/api_guard/jwt_auth/json_web_token.rb:36:in `jwt_and_refresh_token'"
            },
            {
                "id": 9,
                "trace": "api_guard (0.2.1) lib/api_guard/jwt_auth/json_web_token.rb:47:in `create_token_and_set_header'"
            },
            {
                "id": 10,
                "trace": "api_guard (0.2.1) app/controllers/api_guard/registration_controller.rb:10:in `create'"
            },
            {
                "id": 11,
                "trace": "actionpack (5.2.3) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'"
            },
            {
                "id": 12,
                "trace": "actionpack (5.2.3) lib/abstract_controller/base.rb:194:in `process_action'"
            },
            {
                "id": 13,
                "trace": "actionpack (5.2.3) lib/action_controller/metal/rendering.rb:30:in `process_action'"
            },
            {
                "id": 14,
                "trace": "actionpack (5.2.3) lib/abstract_controller/callbacks.rb:42:in `block in process_action'"
            },
            {
                "id": 15,
                "trace": "activesupport (5.2.3) lib/active_support/callbacks.rb:132:in `run_callbacks'"
            },
            {
                "id": 16,
                "trace": "actionpack (5.2.3) lib/abstract_controller/callbacks.rb:41:in `process_action'"
            },
            {
                "id": 17,
                "trace": "actionpack (5.2.3) lib/action_controller/metal/rescue.rb:22:in `process_action'"
            },
            {
                "id": 18,
                "trace": "actionpack (5.2.3) lib/action_controller/metal/instrumentation.rb:34:in `block in process_action'"
            },
            {
                "id": 19,
                "trace": "activesupport (5.2.3) lib/active_support/notifications.rb:168:in `block in instrument'"
            },
            {
                "id": 20,
                "trace": "activesupport (5.2.3) lib/active_support/notifications/instrumenter.rb:23:in `instrument'"
            },
            {
                "id": 21,
                "trace": "activesupport (5.2.3) lib/active_support/notifications.rb:168:in `instrument'"
            },
            {
                "id": 22,
                "trace": "actionpack (5.2.3) lib/action_controller/metal/instrumentation.rb:32:in `process_action'"
            },
            {
                "id": 23,
                "trace": "actionpack (5.2.3) lib/action_controller/metal/params_wrapper.rb:256:in `process_action'"
            },
            {
                "id": 24,
                "trace": "activerecord (5.2.3) lib/active_record/railties/controller_runtime.rb:24:in `process_action'"
            },
            {
                "id": 25,
                "trace": "actionpack (5.2.3) lib/abstract_controller/base.rb:134:in `process'"
            },
            {
                "id": 26,
                "trace": "actionview (5.2.3) lib/action_view/rendering.rb:32:in `process'"
            },
            {
                "id": 27,
                "trace": "actionpack (5.2.3) lib/action_controller/metal.rb:191:in `dispatch'"
            },
            {
                "id": 28,
                "trace": "actionpack (5.2.3) lib/action_controller/metal.rb:252:in `dispatch'"
            },
            {
                "id": 29,
                "trace": "actionpack (5.2.3) lib/action_dispatch/routing/route_set.rb:52:in `dispatch'"
            },
            {
                "id": 30,
                "trace": "actionpack (5.2.3) lib/action_dispatch/routing/route_set.rb:34:in `serve'"
            },
            {
                "id": 31,
                "trace": "actionpack (5.2.3) lib/action_dispatch/routing/mapper.rb:18:in `block in <class:Constraints>'"
            },
            {
                "id": 32,
                "trace": "actionpack (5.2.3) lib/action_dispatch/routing/mapper.rb:48:in `serve'"
            },
            {
                "id": 33,
                "trace": "actionpack (5.2.3) lib/action_dispatch/journey/router.rb:52:in `block in serve'"
            },
            {
                "id": 34,
                "trace": "actionpack (5.2.3) lib/action_dispatch/journey/router.rb:35:in `each'"
            },
            {
                "id": 35,
                "trace": "actionpack (5.2.3) lib/action_dispatch/journey/router.rb:35:in `serve'"
            },
            {
                "id": 36,
                "trace": "actionpack (5.2.3) lib/action_dispatch/routing/route_set.rb:840:in `call'"
            },
            {
                "id": 37,
                "trace": "rack (2.0.7) lib/rack/etag.rb:25:in `call'"
            },
            {
                "id": 38,
                "trace": "rack (2.0.7) lib/rack/conditional_get.rb:38:in `call'"
            },
            {
                "id": 39,
                "trace": "rack (2.0.7) lib/rack/head.rb:12:in `call'"
            },
            {
                "id": 40,
                "trace": "activerecord (5.2.3) lib/active_record/migration.rb:559:in `call'"
            },
            {
                "id": 41,
                "trace": "actionpack (5.2.3) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'"
            },
            {
                "id": 42,
                "trace": "activesupport (5.2.3) lib/active_support/callbacks.rb:98:in `run_callbacks'"
            },
            {
                "id": 43,
                "trace": "actionpack (5.2.3) lib/action_dispatch/middleware/callbacks.rb:26:in `call'"
            },
            {
                "id": 44,
                "trace": "actionpack (5.2.3) lib/action_dispatch/middleware/executor.rb:14:in `call'"
            },
            {
                "id": 45,
                "trace": "actionpack (5.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:61:in `call'"
            },
            {
                "id": 46,
                "trace": "actionpack (5.2.3) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'"
            },
            {
                "id": 47,
                "trace": "railties (5.2.3) lib/rails/rack/logger.rb:38:in `call_app'"
            },
            {
                "id": 48,
                "trace": "railties (5.2.3) lib/rails/rack/logger.rb:26:in `block in call'"
            },
            {
                "id": 49,
                "trace": "activesupport (5.2.3) lib/active_support/tagged_logging.rb:71:in `block in tagged'"
            },
            {
                "id": 50,
                "trace": "activesupport (5.2.3) lib/active_support/tagged_logging.rb:28:in `tagged'"
            },
            {
                "id": 51,
                "trace": "activesupport (5.2.3) lib/active_support/tagged_logging.rb:71:in `tagged'"
            },
            {
                "id": 52,
                "trace": "railties (5.2.3) lib/rails/rack/logger.rb:26:in `call'"
            },
            {
                "id": 53,
                "trace": "actionpack (5.2.3) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'"
            },
            {
                "id": 54,
                "trace": "actionpack (5.2.3) lib/action_dispatch/middleware/request_id.rb:27:in `call'"
            },
            {
                "id": 55,
                "trace": "rack (2.0.7) lib/rack/runtime.rb:22:in `call'"
            },
            {
                "id": 56,
                "trace": "activesupport (5.2.3) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'"
            },
            {
                "id": 57,
                "trace": "actionpack (5.2.3) lib/action_dispatch/middleware/executor.rb:14:in `call'"
            },
            {
                "id": 58,
                "trace": "actionpack (5.2.3) lib/action_dispatch/middleware/static.rb:127:in `call'"
            },
            {
                "id": 59,
                "trace": "rack (2.0.7) lib/rack/sendfile.rb:111:in `call'"
            },
            {
                "id": 60,
                "trace": "railties (5.2.3) lib/rails/engine.rb:524:in `call'"
            },
            {
                "id": 61,
                "trace": "puma (3.12.1) lib/puma/configuration.rb:227:in `call'"
            },
            {
                "id": 62,
                "trace": "puma (3.12.1) lib/puma/server.rb:660:in `handle_request'"
            },
            {
                "id": 63,
                "trace": "puma (3.12.1) lib/puma/server.rb:474:in `process_client'"
            },
            {
                "id": 64,
                "trace": "puma (3.12.1) lib/puma/server.rb:334:in `block in run'"
            },
            {
                "id": 65,
                "trace": "puma (3.12.1) lib/puma/thread_pool.rb:135:in `block in spawn_thread'"
            }
        ],
        "Full Trace": [
            {
                "id": 0,
                "trace": "jwt (2.2.1) lib/jwt/algos/hmac.rb:14:in `digest'"
            },
            {
                "id": 1,
                "trace": "jwt (2.2.1) lib/jwt/algos/hmac.rb:14:in `sign'"
            },
            {
                "id": 2,
                "trace": "jwt (2.2.1) lib/jwt/signature.rb:37:in `sign'"
            },
            {
                "id": 3,
                "trace": "jwt (2.2.1) lib/jwt/encode.rb:57:in `encode_signature'"
            },
            {
                "id": 4,
                "trace": "jwt (2.2.1) lib/jwt/encode.rb:34:in `encoded_signature'"
            },
            {
                "id": 5,
                "trace": "jwt (2.2.1) lib/jwt/encode.rb:20:in `segments'"
            },
            {
                "id": 6,
                "trace": "jwt (2.2.1) lib/jwt.rb:24:in `encode'"
            },
            {
                "id": 7,
                "trace": "api_guard (0.2.1) lib/api_guard/jwt_auth/json_web_token.rb:21:in `encode'"
            },
            {
                "id": 8,
                "trace": "api_guard (0.2.1) lib/api_guard/jwt_auth/json_web_token.rb:36:in `jwt_and_refresh_token'"
            },
            {
                "id": 9,
                "trace": "api_guard (0.2.1) lib/api_guard/jwt_auth/json_web_token.rb:47:in `create_token_and_set_header'"
            },
            {
                "id": 10,
                "trace": "api_guard (0.2.1) app/controllers/api_guard/registration_controller.rb:10:in `create'"
            },
            {
                "id": 11,
                "trace": "actionpack (5.2.3) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'"
            },
            {
                "id": 12,
                "trace": "actionpack (5.2.3) lib/abstract_controller/base.rb:194:in `process_action'"
            },
            {
                "id": 13,
                "trace": "actionpack (5.2.3) lib/action_controller/metal/rendering.rb:30:in `process_action'"
            },
            {
                "id": 14,
                "trace": "actionpack (5.2.3) lib/abstract_controller/callbacks.rb:42:in `block in process_action'"
            },
            {
                "id": 15,
                "trace": "activesupport (5.2.3) lib/active_support/callbacks.rb:132:in `run_callbacks'"
            },
            {
                "id": 16,
                "trace": "actionpack (5.2.3) lib/abstract_controller/callbacks.rb:41:in `process_action'"
            },
            {
                "id": 17,
                "trace": "actionpack (5.2.3) lib/action_controller/metal/rescue.rb:22:in `process_action'"
            },
            {
                "id": 18,
                "trace": "actionpack (5.2.3) lib/action_controller/metal/instrumentation.rb:34:in `block in process_action'"
            },
            {
                "id": 19,
                "trace": "activesupport (5.2.3) lib/active_support/notifications.rb:168:in `block in instrument'"
            },
            {
                "id": 20,
                "trace": "activesupport (5.2.3) lib/active_support/notifications/instrumenter.rb:23:in `instrument'"
            },
            {
                "id": 21,
                "trace": "activesupport (5.2.3) lib/active_support/notifications.rb:168:in `instrument'"
            },
            {
                "id": 22,
                "trace": "actionpack (5.2.3) lib/action_controller/metal/instrumentation.rb:32:in `process_action'"
            },
            {
                "id": 23,
                "trace": "actionpack (5.2.3) lib/action_controller/metal/params_wrapper.rb:256:in `process_action'"
            },
            {
                "id": 24,
                "trace": "activerecord (5.2.3) lib/active_record/railties/controller_runtime.rb:24:in `process_action'"
            },
            {
                "id": 25,
                "trace": "actionpack (5.2.3) lib/abstract_controller/base.rb:134:in `process'"
            },
            {
                "id": 26,
                "trace": "actionview (5.2.3) lib/action_view/rendering.rb:32:in `process'"
            },
            {
                "id": 27,
                "trace": "actionpack (5.2.3) lib/action_controller/metal.rb:191:in `dispatch'"
            },
            {
                "id": 28,
                "trace": "actionpack (5.2.3) lib/action_controller/metal.rb:252:in `dispatch'"
            },
            {
                "id": 29,
                "trace": "actionpack (5.2.3) lib/action_dispatch/routing/route_set.rb:52:in `dispatch'"
            },
            {
                "id": 30,
                "trace": "actionpack (5.2.3) lib/action_dispatch/routing/route_set.rb:34:in `serve'"
            },
            {
                "id": 31,
                "trace": "actionpack (5.2.3) lib/action_dispatch/routing/mapper.rb:18:in `block in <class:Constraints>'"
            },
            {
                "id": 32,
                "trace": "actionpack (5.2.3) lib/action_dispatch/routing/mapper.rb:48:in `serve'"
            },
            {
                "id": 33,
                "trace": "actionpack (5.2.3) lib/action_dispatch/journey/router.rb:52:in `block in serve'"
            },
            {
                "id": 34,
                "trace": "actionpack (5.2.3) lib/action_dispatch/journey/router.rb:35:in `each'"
            },
            {
                "id": 35,
                "trace": "actionpack (5.2.3) lib/action_dispatch/journey/router.rb:35:in `serve'"
            },
            {
                "id": 36,
                "trace": "actionpack (5.2.3) lib/action_dispatch/routing/route_set.rb:840:in `call'"
            },
            {
                "id": 37,
                "trace": "rack (2.0.7) lib/rack/etag.rb:25:in `call'"
            },
            {
                "id": 38,
                "trace": "rack (2.0.7) lib/rack/conditional_get.rb:38:in `call'"
            },
            {
                "id": 39,
                "trace": "rack (2.0.7) lib/rack/head.rb:12:in `call'"
            },
            {
                "id": 40,
                "trace": "activerecord (5.2.3) lib/active_record/migration.rb:559:in `call'"
            },
            {
                "id": 41,
                "trace": "actionpack (5.2.3) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'"
            },
            {
                "id": 42,
                "trace": "activesupport (5.2.3) lib/active_support/callbacks.rb:98:in `run_callbacks'"
            },
            {
                "id": 43,
                "trace": "actionpack (5.2.3) lib/action_dispatch/middleware/callbacks.rb:26:in `call'"
            },
            {
                "id": 44,
                "trace": "actionpack (5.2.3) lib/action_dispatch/middleware/executor.rb:14:in `call'"
            },
            {
                "id": 45,
                "trace": "actionpack (5.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:61:in `call'"
            },
            {
                "id": 46,
                "trace": "actionpack (5.2.3) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'"
            },
            {
                "id": 47,
                "trace": "railties (5.2.3) lib/rails/rack/logger.rb:38:in `call_app'"
            },
            {
                "id": 48,
                "trace": "railties (5.2.3) lib/rails/rack/logger.rb:26:in `block in call'"
            },
            {
                "id": 49,
                "trace": "activesupport (5.2.3) lib/active_support/tagged_logging.rb:71:in `block in tagged'"
            },
            {
                "id": 50,
                "trace": "activesupport (5.2.3) lib/active_support/tagged_logging.rb:28:in `tagged'"
            },
            {
                "id": 51,
                "trace": "activesupport (5.2.3) lib/active_support/tagged_logging.rb:71:in `tagged'"
            },
            {
                "id": 52,
                "trace": "railties (5.2.3) lib/rails/rack/logger.rb:26:in `call'"
            },
            {
                "id": 53,
                "trace": "actionpack (5.2.3) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'"
            },
            {
                "id": 54,
                "trace": "actionpack (5.2.3) lib/action_dispatch/middleware/request_id.rb:27:in `call'"
            },
            {
                "id": 55,
                "trace": "rack (2.0.7) lib/rack/runtime.rb:22:in `call'"
            },
            {
                "id": 56,
                "trace": "activesupport (5.2.3) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'"
            },
            {
                "id": 57,
                "trace": "actionpack (5.2.3) lib/action_dispatch/middleware/executor.rb:14:in `call'"
            },
            {
                "id": 58,
                "trace": "actionpack (5.2.3) lib/action_dispatch/middleware/static.rb:127:in `call'"
            },
            {
                "id": 59,
                "trace": "rack (2.0.7) lib/rack/sendfile.rb:111:in `call'"
            },
            {
                "id": 60,
                "trace": "railties (5.2.3) lib/rails/engine.rb:524:in `call'"
            },
            {
                "id": 61,
                "trace": "puma (3.12.1) lib/puma/configuration.rb:227:in `call'"
            },
            {
                "id": 62,
                "trace": "puma (3.12.1) lib/puma/server.rb:660:in `handle_request'"
            },
            {
                "id": 63,
                "trace": "puma (3.12.1) lib/puma/server.rb:474:in `process_client'"
            },
            {
                "id": 64,
                "trace": "puma (3.12.1) lib/puma/server.rb:334:in `block in run'"
            },
            {
                "id": 65,
                "trace": "puma (3.12.1) lib/puma/thread_pool.rb:135:in `block in spawn_thread'"
            }
        ]
    }
}```

@powerfulsheron Thanks for taking your time to get the error log.

From the error log I can see that the value of ApiGuard.token_signing_secret is nil. By default, it should take secret_key_base from Rails secrets and I not sure how it is empty.

Can you please let me know the Rails and Ruby version of your app?

Hi @Gokul595

Yes, i'm on
Rails 5.2.3
ruby 2.6.2p47 (2019-03-13 revision 67232) [x86_64-linux]

And I'm using this docker :
https://github.com/LouisHrg/ror-vuejs-docker

@powerfulsheron It has been months. I am sorry for the delay.

I hope updating to v0.2.2 should fix this issue. Please check and let me know.