Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught error when axios retry is used #1075

Closed
4 of 5 tasks
adambullmer opened this issue Feb 24, 2021 · 4 comments
Closed
4 of 5 tasks

Uncaught error when axios retry is used #1075

adambullmer opened this issue Feb 24, 2021 · 4 comments
Labels

Comments

@adambullmer
Copy link

Version

module: 5.0.0-1613647907.37b1156
nuxt: 2.14.12

Nuxt configuration

mode:

  • universal
  • spa

Nuxt configuration

export default {
  axios: {
    retry: true,
  },
}

Reproduction

⚠️ without a minimal reproduction we won't be able to look into your issue

Reproducible example: https://codesandbox.io/s/busy-gareth-vewyu

What is expected?

When the native @nuxtjs/axios module is configured for retrying, the auth module should still work on a retried request.

What is actually happening?

An error is thrown on SSR regarding checking the Authorization header. If using nuxt error pages, this will instead render an error page instead of the intended page. This is due to getting a different type config object structure in the headers.

Steps to reproduce

In the above example, just visiting the home page directly by address bar or refreshing the page will yield the server error.

 ERROR  Cannot read property 'Authorization' of undefined             18:50:18

  at RequestHandler._requestHasAuthorizationHeader (server.js:4001:35)
  at interceptor.axios.interceptors.request.use (server.js:3978:28)

Additional information

I believe the root of the bug is here, where deep object accessors don't check that the previous key exists: https://github.com/nuxt-community/auth-module/blob/dev/src/inc/request-handler.ts#L126

I've made a patch locally to not be blocked by a release:

diff --git a/dist/runtime.js b/dist/runtime.js
index 62e6ede5118ed93c1135ff13f40e3682e023842e..a3d9a7d3de48e8ca62fb19a01546c3a6b75e0df7 100644
--- a/dist/runtime.js
+++ b/dist/runtime.js
@@ -813,7 +813,9 @@ class RequestHandler {
     return config;
   }
   _requestHasAuthorizationHeader(config) {
-    return !!config.headers.common[this.scheme.options.token.name];
+    return config.headers.common
+      ? !!config.headers.common[this.scheme.options.token.name]
+      : !!config.headers[this.scheme.options.token.name];
   }
 }
 
diff --git a/dist/runtime.mjs b/dist/runtime.mjs
index d31247774cf349475b81392852771de6995d48c7..9acc35841b61c6a4661571c2961b7f5561741f0e 100644
--- a/dist/runtime.mjs
+++ b/dist/runtime.mjs
@@ -802,7 +802,9 @@ class RequestHandler {
     return config;
   }
   _requestHasAuthorizationHeader(config) {
-    return !!config.headers.common[this.scheme.options.token.name];
+    return config.headers.common
+      ? !!config.headers.common[this.scheme.options.token.name]
+      : !!config.headers[this.scheme.options.token.name];
   }
 }

Checklist

  • I have tested with the latest Nuxt version and the issue still occurs
  • I have tested with the latest module version and the issue still occurs
  • I have searched the issue tracker and this issue hasn't been reported yet

Steps to reproduce

What is expected?

What is actually happening?

Screen Shot 2021-02-24 at 1 11 22 PM

Performance analysis?

n/a

@rico-ocepek
Copy link

I'm currently experiencing the same error but with a different Nuxt configuration.

Did you discover any workarounds that can be done in userland i.e. that don't require patching the module?

@mattprado21
Copy link

@rico-ocepek I also encountered this issue while setting up the auth for local login. For workarounds, use @nuxt/auth which is the previous version on nuxt auth(4.9.1) instead of @nuxt/auth-next.

@bmulholland
Copy link
Contributor

We recently upgraded axios, can you please try the latest auth-next and see if this is resolved?

@bmulholland
Copy link
Contributor

Also, for SSR, please check everything listed in #1197

@nuxt-community nuxt-community locked and limited conversation to collaborators Jan 24, 2022
@bmulholland bmulholland converted this issue into discussion #1624 Jan 24, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests

4 participants