Refresh JWT Strategy - Fetch
Unanswered
Lesser Yellowlegs posted this in #help-forum
Lesser YellowlegsOP
We're trying to make a request on behalf of a user in a
We have the following code to
1) Make a request
2) Add auth header if access token exists
3) Check for a
4) Remake request(not present in this snippit)
Problem being the
not actual working code, taken from a playground we have
server component. We have the following code to
1) Make a request
2) Add auth header if access token exists
3) Check for a
401 and refresh tokens if needed4) Remake request(not present in this snippit)
Problem being the
/api/refresh route is not setting the cookie I presume because it is made server side instead of client. But since the request is run server side; we're a bit confused on the best practice to enforce token rotations in NextJS 13.not actual working code, taken from a playground we have
const { GET } = createClient<paths>({
baseUrl: "http://localhost:5000",
async fetch(input, init) {
// fetch http cookie
let accessToken = cookies().get("access-token")?.value;
// if token exists, add to header
if (init && accessToken) {
init.headers = {
"content-type": "application/json",
authorization: `Bearer ${accessToken}`,
...init.headers
};
}
const response = await fetch(input, init);
// Update Refresh and Access Token
// and remake previous request
if (response.status === 401) {
await fetch("http://localhost:3000/api/refresh");
}
return response;
}
});1 Reply
Lesser YellowlegsOP
bump