Deleting cookie based on response status from external api #77293
Unanswered
zwelhtetyan
asked this question in
Help
Replies: 1 comment 1 reply
-
My current way to delete token from cookie is that I redirect to fake url and check that url in middleware and handle removing logic by calling server action. Please let me know if you have better solution. Here is the code. export const api = ky.create({
prefixUrl: BASE_URL,
hooks: {
beforeRequest: [injectTokenBeforeRequest],
beforeError: [
async (error) => {
const { response } = error
if (response?.status === 401) {
redirect(/invalid-token)
}
return error
},
],
},
}) // middleware.ts
export async function middleware(request: NextRequest) {
const path = request.nextUrl.pathname
if (path.startsWith("/invalid-token")) {
await deleteSessionToken() // calling server action
return NextResponse.redirect(new URL("/signin", request.nextUrl))
}
return NextResponse.next()
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
How can I delete a cookie when the token is invalid on the server? I use Ky for data fetching. Currently, I check the error status in Ky's response interceptor, and if the status is 401, I log out the user. However, since cookies can't be modified on the server from the client-side interceptor, I can't call both a server action and a route handler within the interceptor. What is the best approach to deleting a cookie when receiving a 401 response?
Additional information
No response
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions