Forráskód Böngészése

接口提示未授权token后的退出登录逻辑优化

fogwind 1 napja
szülő
commit
51bafac9a1

+ 2 - 1
src/actions/logoutAction.ts

@@ -22,10 +22,11 @@ export async function logoutAction() {
         } else {
             success = res.data?.createLogout?.logout?.success ?? false;
             msg = res.data?.createLogout?.logout?.message ?? "";
-            if(success) {
+            if(success || (!success && msg === "Unauthenticated. Please login to perform this action")) {
                 const cookieStore = await cookies();
                 // 设置游客状态
                 cookieStore.set(IS_GUEST, "true");
+                success = true;
             }
         }
 

+ 7 - 19
src/app/(checkout)/error.tsx

@@ -1,19 +1,18 @@
 "use client";
 import { useEffect } from 'react'
 import { useRouter } from 'next/navigation';
-import { signOut } from "next-auth/react";
-import { useAppDispatch } from "@/store/hooks";
-import { clearCart } from "@/store/slices/cart-slice";
+import { emitAuthExpired } from "@/utils/auth/auth-events";
+
 //https://nextjs.org/docs/app/api-reference/file-conventions/error
 export default function Error({
   error, 
-  reset
+  retry
 }: {
-  reset: () => void;
+  retry: () => void;
   error: Error & { digest?: string };
 }) {
   const router = useRouter();
-  const dispatch = useAppDispatch();
+
   let msg = "There was an issue with our storefront. This could be a temporary issue, please try your action again.";
   if(error.name === 'UnauthorizedError') {
     msg = "Authorization Bearer token is required. Will redirect to Login page.";
@@ -22,23 +21,12 @@ export default function Error({
       if(error.name === 'UnauthorizedError') {
           router.push('/customer/login')
       } else {
-        reset();
+        retry();
       }
   };
   useEffect(() => {
       if (error.name === "UnauthorizedError") {
-            signOut({
-              callbackUrl:"/customer/login",
-              redirect: false,
-            }).then(() => {
-                dispatch(clearCart());
-     
-                setTimeout(() => {
-                  router.push("/customer/login");
-                  router.refresh();
-                }, 100);
-            });
-
+            emitAuthExpired();
       }
     // Log the error to an error reporting service
     console.error(error)

+ 7 - 19
src/app/(public)/error.tsx

@@ -1,19 +1,18 @@
 "use client";
 import { useEffect } from 'react'
 import { useRouter } from 'next/navigation';
-import { signOut } from "next-auth/react";
-import { useAppDispatch } from "@/store/hooks";
-import { clearCart } from "@/store/slices/cart-slice";
+import { emitAuthExpired } from "@/utils/auth/auth-events";
+
 //https://nextjs.org/docs/app/api-reference/file-conventions/error
 export default function Error({
   error, 
-  reset
+  retry
 }: {
-  reset: () => void;
+  retry: () => void;
   error: Error & { digest?: string };
 }) {
   const router = useRouter();
-  const dispatch = useAppDispatch();
+
   let msg = error.message || "There was an issue with our storefront. This could be a temporary issue, please try your action again.";
   if(error.name === 'UnauthorizedError') {
     msg = "Authorization Bearer token is required. Will redirect to Login page.";
@@ -22,23 +21,12 @@ export default function Error({
       if(error.name === 'UnauthorizedError') {
           router.push('/customer/login')
       } else {
-        reset();
+        retry();
       }
   };
   useEffect(() => {
       if (error.name === "UnauthorizedError") {
-            signOut({
-              callbackUrl:"/customer/login",
-              redirect: false,
-            }).then(() => {
-                dispatch(clearCart());
-     
-                setTimeout(() => {
-                  router.push("/customer/login");
-                  router.refresh();
-                }, 100);
-            });
-
+            emitAuthExpired();
       }
     // Log the error to an error reporting service
     console.error(error)

+ 2 - 0
src/components/customer/AuthSessionGuard.tsx

@@ -6,6 +6,7 @@ import {
     useRef,
 } from "react";
 import { signOut } from "next-auth/react";
+import { logoutAction } from "@/actions";
 import { useApolloClient } from "@apollo/client/react";
 import {
     AUTH_EXPIRED_EVENT,
@@ -39,6 +40,7 @@ export default function AuthSessionGuard() {
             signingOutRef.current = true;
 
             try {
+                await logoutAction();
 
                 /**
                  * 1. 清除 NextAuth session