最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - Next js react query v5 useSuspenseQuery, 401 unathorized - Stack Overflow

programmeradmin1浏览0评论

I want to use useSuspenseQuery along with next js so that I can take advantage of the built-in React feature.

But when using useSuspenseQuery, it gives an error img

query

const { data, isLoading } = useSuspenseQuery({
        queryKey: ["user"],
        queryFn: () => {
            return (userService.getUserById(Number(id)))},
    });

axios interceptor

axiosWithAuth.interceptors.response.use(
    (config) => config,
    async (error) => {
        const originalRequest = error.config;

        if (
            (error?.response?.status === 401 ||
                errorCatch(error) === "jwt expried" ||
                errorCatch(error) === "jwt must be provided") &&
            error.config &&
            !error.config._isRetry
        ) {
            originalRequest._isRetry = true;
            try {
                const { accessToken } = await authService.getNewTokens();

                originalRequest.headers[
                    "Authorization"
                ] = `Bearer ${accessToken}`;
                
                return axiosWithAuth.request(originalRequest);
            } catch (error) {
                if (errorCatch(error) === "jwt expired") removeFromStorage();
            }
        }

        throw error;
    }
);

query with axios

  async getUserById(userId: number) {
        const response = await axiosWithAuth<IUserTg>({
            url: API_URL.user(`/${userId}`),
            method: "GET",
        });
        return response.data;
    }

When switching between pages (when routing), it does not give such an error, but if you refresh the page itself, it will give

if you use use Query, then everything works as it should (but I need useSuspenseQuery) if you disable authorization verification on the server, then everything works fine, without errors (but I need the request to be authorized)

I want to use useSuspenseQuery along with next js so that I can take advantage of the built-in React feature.

But when using useSuspenseQuery, it gives an error img

query

const { data, isLoading } = useSuspenseQuery({
        queryKey: ["user"],
        queryFn: () => {
            return (userService.getUserById(Number(id)))},
    });

axios interceptor

axiosWithAuth.interceptors.response.use(
    (config) => config,
    async (error) => {
        const originalRequest = error.config;

        if (
            (error?.response?.status === 401 ||
                errorCatch(error) === "jwt expried" ||
                errorCatch(error) === "jwt must be provided") &&
            error.config &&
            !error.config._isRetry
        ) {
            originalRequest._isRetry = true;
            try {
                const { accessToken } = await authService.getNewTokens();

                originalRequest.headers[
                    "Authorization"
                ] = `Bearer ${accessToken}`;
                
                return axiosWithAuth.request(originalRequest);
            } catch (error) {
                if (errorCatch(error) === "jwt expired") removeFromStorage();
            }
        }

        throw error;
    }
);

query with axios

  async getUserById(userId: number) {
        const response = await axiosWithAuth<IUserTg>({
            url: API_URL.user(`/${userId}`),
            method: "GET",
        });
        return response.data;
    }

When switching between pages (when routing), it does not give such an error, but if you refresh the page itself, it will give

if you use use Query, then everything works as it should (but I need useSuspenseQuery) if you disable authorization verification on the server, then everything works fine, without errors (but I need the request to be authorized)

Share Improve this question asked Feb 5 at 15:43 И ИИ И 1
Add a comment  | 

1 Answer 1

Reset to default 0

This is likely due to Next attempting to pre-render the Suspense boundary, despite the useSuspenseQuery falling under a client component (https://nextjs.org/docs/app/building-your-application/rendering/client-components) -- since the pre-render attempt is missing the client's headers/cookies, it is presumably failing your authorization.

Some notes on Suspense behavior with useSuspenseQuery can be found here: https://tanstack.com/query/latest/docs/framework/react/guides/ssr#a-quick-note-on-suspense.

发布评论

评论列表(0)

  1. 暂无评论