Diizzayy / nuxt-graphql-client

⚡️ Minimal GraphQL Client + Code Generation for Nuxt3
https://nuxt-graphql-client.web.app
MIT License
366 stars 44 forks source link

bug: when you get a nuxt `error` then go back. #267

Closed cpreston321 closed 1 year ago

cpreston321 commented 1 year ago

Environment

Describe the bug

When you have a plugin defined that injects the authToken to nuxt-grapqhl-client and you get any 404 page then go back the hook doesn't re-inject the token and have to do a manual refresh.

Example Hook:

nuxtApp.hook('gql:auth:init', ({ token }) => {
    token.value = `${cookieToken.value}`
  })

Expected behaviour

When you get an error it should redirect back home and inject the token correctly using the hook properly.

Reproduction

No response

Additional context

I need to find a graphql endpoint that has auth to setup the reproduction for this as I don't know any of the top of my head.

Logs

No response

cpreston321 commented 1 year ago

@Diizzayy Temporary solution:

nuxtApp.hook('gql:auth:init', async ({ token }) => {
    const ssrEvent = nuxtApp.ssrContext?.event
    if (ssrEvent) {
      token.value = ssrEvent.context.auth.token
    }
    else {
      const cookieToken = await $fetch('/api/get-cookie')
      token.value = `${cookieToken}`
    }
  })
Diizzayy commented 1 year ago

@cpreston321 The gql:auth:init hook is meant to declare custom logic for retrieving and applying an auth token when needed. This module provides a default mechanism for handling cookie auth that meets the needs of most applications.

you get any 404 page then go back the hook doesn't re-inject the token and have to do a manual refresh.

The gql:auth:init hook is triggered anytime a request made.

It sounds like the issue you encountered here is solely due to your cookie being inaccessible by the browser ( hence why you've created the /api/get-cookie route I assume )

cpreston321 commented 1 year ago

That is correct, the cookie is a server side cookie with I set within context of the router. I still need a way to grab the token so I created a API endpoint for the client side of the app.

I also don't want to create another token with the same value -- I want to leverage what I already have to save from duplication.

Diizzayy commented 1 year ago

That is correct, the cookie is a server side cookie with I set within context of the router. I still need a way to grab the token so I created a API endpoint for the client side of the app.

@cpreston321 Okay I understand, I'd suggest using a server plugin to check for the cookie's existence and pass along the token to your app. That would negate the need for the additional request to the get-cookie route.

You'd then be able to access the extracted cookie value within the gql:auth:init hook, If I understand your use case correctly. I'd also suggest disabling tokenStorage

Feel free to reach out to me on discord, I'd be happy to have a closer look and help you find the best resolution here.

cpreston321 commented 1 year ago

@Diizzayy what is your discord tag?

Diizzayy commented 1 year ago

@cpreston321 you can reach me at Diizzayy#1964

cpreston321 commented 1 year ago

@Diizzayy sent as officialcpzz