Next.js Discord

Discord Forum

Preventing a token from regeneration before it's expiry time

Unanswered
West African Lion posted this in #help-forum
Open in Discord
West African LionOP
I use an api to generate a token and this token is later on used as header for fetching plans from another api. The problem that I am facing is that with my code, I am regenerating token before its expiration.
What I want to achieve is that if the token is not expired, I want to reuse it for fetching plans. How can I achieve this?

39 Replies

@ihyd Just, add logic to validate the token? if it's valid, proceed, else regenerate a new token / reauthenticate
West African LionOP
So, I have to implement this in page.tsx?
wherever you check your token
in middleware
West African LionOP
I have used the middleware. Here's my codebase for middleware.tsx.
And here's the error that I am getting:
Error: invariant: cookies() expects to have requestasyncstorage, none available.
And yes, one more thing, the version that I am allowed to use is Next@13.5.1
you're exposing session tokens and API keys, consider setting those as environment values instead of hardcoding
the error is here?
did you import them
import { cookies } from 'next/headers'
West African LionOP
Yes, I did import them.
@ihyd did you import them
West African LionOP
Yes, I did import them.
@West African Lion Click to see attachment
could you show the full code here?
for app/page.tsx
@Ray could you show the full code here?
West African LionOP
Here's the code for app/page.tsx
@West African Lion Here's the code for app/page.tsx
still get the error with this?
West African LionOP
@Ray still get the error with this?
West African LionOP
Yes, the Next version that is allowed to use is Next@13.5.1
@West African Lion Click to see attachment
remove 'use server' inside fetchData
West African LionOP
still the error persists
@Ray remove `'use server'` inside `fetchData`
West African LionOP
The error still persists!
hard to see in screenshot
is it on github?
West African LionOP
No!
or stackblitz?
West African LionOP
It is on stackblitz.
@Ray or stackblitz?
West African LionOP
But, it is the exam
could you share the link?
West African LionOP
Give me a moment!
@Ray could you share the link?
West African LionOP
I can share it to you in DM!
look like you have 'use client' on top of components/plans.tsx?
oh nm
yeah share me the link
no error on your link