redirect() in axios interceptor failing
Unanswered
kylemh posted this in #help-forum
kylemhOP
hey folks!
i've got the following code:
and the redirect() call is consistently failing. The stack trace:
The error class logged:
Any clues?
i've got the following code:
import 'client-only';
import { fetchAuthSession } from 'aws-amplify/auth';
import axios from 'axios';
import { redirect } from 'next/navigation';
import type { AxiosError } from 'axios';
export const axiosClient = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_URL,
headers: {
'Content-Type': 'application/json',
},
});
axiosClient.interceptors.response.use(
(response) => response,
(error: AxiosError) => {
const status = error?.response?.status ?? error?.status;
const isInvalidAuth = status === 401;
if (isInvalidAuth) {
try {
redirect('/logout');
} catch (error_) {
console.log(error_);
}
}
return Promise.reject(error);
},
);
and the redirect() call is consistently failing. The stack trace:
Error: NEXT_REDIRECT
at getRedirectError (redirect.ts:21:17)
at redirect (redirect.ts:47:9)
at eval (pharmakonClient.ts:46:16)
at async Axios.request (Axios.js:40:14)
at async useCouponSearch.useQuery (useCoupons.ts:75:24)
The error class logged:
{
digest: "NEXT_REDIRECT;replace;/logout;307;",
message: "NEXT_REDIRECT",
stack: "Error: NEXT_REDIRECT\n at getRedirectError (http://localhost:3000/_next/static/chunks/043e8_next_dist_client_6728f717._.js:4458:41)\n at redirect (http://localhost:3000/_next/static/chunks/043e8_next_dist_client_6728f717._.js:4469:11)\n at eval (http://localhost:3000/_next/static/chunks/src_ee4eb5a0._.js?id=%255Bproject%255D%252Fsrc%252Fclients%252FclientSide%252FpharmakonClient.ts+%255Bapp-client%255D+%2528ecmascript%2529:46:325)\n at async Axios.request (http://localhost:3000/_next/static/chunks/node_modules__pnpm_cfa412c1._.js:8884:20)\n at async useCouponSearch.useQuery (http://localhost:3000/_next/static/chunks/src_ee4eb5a0._.js:591:34)",
__NEXT_ERROR_CODE: "E394"
}
Any clues?
1 Reply
kylemhOP
Since this interceptor is only for client-side requests, my work-around is just to mutate
window.location.href