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

useGqlToken/useHeaders with SSR #216

Closed nikitin-dev closed 2 years ago

nikitin-dev commented 2 years ago

Hi there! I have a problem with reassigning request headers. I'll start by describing my situation. I am using Nuxt version 3.0.0-rс.9 and strapi for the backend. In cookies, I have a JWT token, which is written after user authorization. Strapi provides the ability to request user data by forwarding a header with a JWT token. I want to receive information about the user's authentication status on the server side and, depending on the response, render the necessary markup on the server as well.

Here is my script in app.vue

<script lang="ts" setup>
import { useAuthStore } from "~~/store/auth";

const authStore = useAuthStore();
const jwt = useCookie("jwt");

const userAuth = () => {
  if (jwt.value) {
    useGqlToken(jwt.value);
    return GqlUser();
  }
};

const { data: user } = await useAsyncData("user", userAuth);

if (user.value?.me) {
  authStore.authUser({
    jwt: jwt.value,
    user: user.value.me
  });
}
</script>

As you see, i'am trying to reassign header using "useGqlToken" each time. The problem is: When i authorize the user №1, i get JWT, reload page and it works. My user is authorized and markup was rendered on the server. But when i logout and authorize user №2, i also get ANOTHER JWT token, reload page and i get data of user №1.

This is strange behavior, because in fact request must be executed with a new one JWT token. I dived into source code and console logged some stuff:

Action 1:

  1. User №1 authorization
  2. Reloading page

image

As you can see, we get valid jwt token in the useGqlToken function and valid request header in the setGqlState function as well In this case, i get expected result i.e. data of user №1.

Action 2:

  1. User №1 logout (jwt removed from cookies)
  2. User №2 authorization
  3. Reload page

image

We also get valid jwt token in the useGqlToken function and valid request header in the setGqlState function as well But in this case i get data of user №1 AGAIN.

The first log, named "TOKEN" it is: image

The second log, named INSTANCE it is result of "setGqlState" function execution: image

It looks like it's not working for reassigning token in headers. Or am I just doing something wrong?

Thanks in advance, looking forward to your reply)

Diizzayy commented 2 years ago

@nikitin-dev I've been unable to replicate this behavior locally.

Any additional details you may be able to provide, or perhaps a reproduction would be a great help in addressing this

Diizzayy commented 2 years ago

@nikitin-dev This should be fixed as of v0.1.28

nikitin-dev commented 2 years ago

That solved my issue. @Diizzayy, thanks a lot! You're really awesome guy. Thanks for your productive work and quick replies!

Diizzayy commented 2 years ago

@nikitin-dev Happy to help!