Server data fetching with custom backend
Unanswered
cod.rian posted this in #help-forum
cod.rianOP
I'm struggling to make it work so i decided to write...
I need to use a custom backend (.net) in a Nextjs app router project.
Everything works for non authenticated requests to my .net backend (public resources), data fetch on the server, caching, etc...
But once i need to do authenticated requests (access and refresh token, with refresh token rotation) everything explodes and can't find a proper pattern, my biggest problem is i can not refresh the cookie with the tokens in case the request fails.
So my question is... is it possible to server-side fetching for authenticated requests (authorization bearer token), handling possible refresh need, etc.?
Should this be done by client side components only?
I would like to keep server side fetching features and make it work with my custom backend authentication.
Any pattern, code example would be awesome. massive thanks.
I need to use a custom backend (.net) in a Nextjs app router project.
Everything works for non authenticated requests to my .net backend (public resources), data fetch on the server, caching, etc...
But once i need to do authenticated requests (access and refresh token, with refresh token rotation) everything explodes and can't find a proper pattern, my biggest problem is i can not refresh the cookie with the tokens in case the request fails.
So my question is... is it possible to server-side fetching for authenticated requests (authorization bearer token), handling possible refresh need, etc.?
Should this be done by client side components only?
I would like to keep server side fetching features and make it work with my custom backend authentication.
Any pattern, code example would be awesome. massive thanks.
5 Replies
cod.rianOP
@Yi Lon Ma would you enlighten me? i just saw you helping someone with this 2 months ago.
cod.rianOP
i mean, a request that is done with an expired access token. (fails with a status code 401 by design)
i think i find a possible way to solve this, redirect to a route handler that refreshes tokens and then redirects back to the page where the request failed. gonna try to add it and see how it goes, I'll pay for the beer if it works :p
set expiry date of cookies, use
await cookies()
to fetch cookies and send request, if the cookie doesn't exist/is expired, use your refresh logic to get new access token