Handle Token with AuthJS
Unanswered
Banjara Hound posted this in #help-forum
Banjara HoundOP
i have api that send token to client when auth is verified. i want a way to handle this with authjs or any other package so when access token is invalid use refresh token to get new data from api.
i tried manually set and update token in httpOnly cookies in middleware but it's failed since it's request 4 or 5 times at a same time to api server and got too many request.
i only need token in server actions so i want a way to get and set tokens in server action that call directly or using form.
my custom way only work when form is submited.
Update: my own way only work for first time after that it's not work. and i get 401
i tried manually set and update token in httpOnly cookies in middleware but it's failed since it's request 4 or 5 times at a same time to api server and got too many request.
i only need token in server actions so i want a way to get and set tokens in server action that call directly or using form.
my custom way only work when form is submited.
Update: my own way only work for first time after that it's not work. and i get 401
2 Replies
Banjara HoundOP
i found out that with matcher we can tell middleware when to run and execute.
this code solve my problem with middleware. no need to custom package my own way work fine. just have to check it in real scenario that see it will work or not.
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
'/((?!api|_next/static|_next/image|favicon.ico).*)',
],
}this code solve my problem with middleware. no need to custom package my own way work fine. just have to check it in real scenario that see it will work or not.
Banjara HoundOP
it's work fine in real scenario except for when token is updated (in response) and is not in request. is that possible to fix this?