#Cookies needs time to set cookies after hard reload

2 messages · Page 1 of 1 (latest)

dull whale
#

export default defineEventHandler(async (event) => {
  console.log('REQUEST WAS MADE');
  const cookies = parseCookies(event);
  console.log(cookies);
  if (cookies.jwtToken) {
    try {
      const userId = (await decryptToken(cookies.jwtToken)) as string;
      event.context.userId = userId;
    } catch (error) {
      console.log(error);
    }
  } else {
    console.log('NOT LOGGED IN');
  }
});
``` my auth middleware for my api route. If i refresh my site the first few requests in the first ms are without the headers. This is my template ```<template>
  <div>
    {{ user }}
  </div>
</template>

<script setup lang="ts">
const { data: user } = await useFetch("/api/user/details");
definePageMeta({
  middleware: ["auth"],
});
</script>

<style scoped>
</style>```  Workaround right now is to set a timeout which runs after 1000ms, ```const user = ref("Loading");
setTimeout(async () => {
  console.log("fetch user");
  const { data } = await useFetch("/api/user/details");
  user.value = data.value?.user;
}, 1000);``` This is working but i think there has to be another way
wintry trench
#

I came across this too - where useCookie takes longer than expected to finish. My workaround was also a short 250ms timeout. Maybe there's a way to wrap the call for the cookie in a Promise and await it?