question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Global access token not used on client-side navigation

See original GitHub issue

Hello!

I’ve been experimenting with nuxt3 and directus using your module to call the graphql endpoint. I have set up the .env like so:

GQL_HOST="http://localhost:8055/graphql"
GQL_TOKEN="MY_TOKEN"

I then created my request files and asyncData fetching like in the docs:

// pages/posts/index.vue

const { data } = await useAsyncData('posts', () => GqlPosts())

This works great!

However, when doing the same thing inside a dynamic route page, it seems like the access token is no longer used. I get an error 400. Adding useGqlToken('MY_TOKEN') resolves this but I guess it is not intended.

// pages/posts/[slug].vue

useGqlToken('MY_TOKEN')
const { data } = await useAsyncData('post-' + UID, () => GqlUniquePost({
  slug: route.params.slug,
  }))

Am I doing something wrong? Thanks a lot!

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Diizzayycommented, May 6, 2022

Is it supposed to be resolved by using the variables inside runtimeConfig?

Not quite, manually adding the token to runtimeConfig is equivalent to using an env variable.

I think it would be favorable but I’m not entirely sure as I plan to use nuxt generate when fully supported. I would not want you to work on this if nuxt generate will not be supported!

Using nuxt generate would certainly get around this issue, though you’d still need to make client-side authenticated requests during development.

I think there is merit in adding the retainToken flag as a bypass for the current behavior. If anything, we can always revise this at a later point.

0reactions
narduincommented, May 6, 2022

Oh I understand, this is sane behaviour. I just realized that it’s the same with non-dynamic routes actually. Is it supposed to be resolved by using the variables inside runtimeConfig? I tried this but nothing changed:

// nuxt.config.ts

  runtimeConfig: {
    public: {
      'graphql-client': {
        clients: {
          default: {
            host: 'http://localhost:8055/graphql',
            token: 'MY_TOKEN'
          }
        }
      }
    }
  }

@Diizzayy, I think it would be favorable but I’m not entirely sure as I plan to use nuxt generate when fully supported. I would not want you to work on this if nuxt generate will not be supported!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using OAuth 2.0 for client-side web applications
This document explains how to implement OAuth 2.0 authorization to access the YouTube Data API from a JavaScript web application.
Read more >
how to deal with access token and refresh token in client side
I am developing a service layer for both browser-based and non-browser clients. I plan to use JWT (JSON Web Token) to authenticate both....
Read more >
The Ultimate Guide to handling JWTs on frontend clients ...
The Ultimate Guide to handling JWTs on frontend clients (GraphQL). JWTs (JSON Web Token, pronounced 'jot') are becoming a popular way of ...
Read more >
Add a way to persist oAuth access token · Issue #39 - GitHub
This is because the oAuth2 access token is not currently persisted anywhere, it is just in memory. This document recommends against storing the...
Read more >
How to Store Session Tokens in a Browser (and the impacts of ...
The simplest way to achieve this is to just use a top level global variable. This variable will be available to any of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found