logoutAction.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use server";
  2. import {cookies} from "next/headers";
  3. import { IS_GUEST } from "@/utils/constants";
  4. import { serverGraphqlFetch } from "@utils/bagisto";
  5. import {CUSTOMER_LOGOUT} from "@/graphql/customer/mutations";
  6. import type { LogoutData } from "@/types/types";
  7. export async function logoutAction() {
  8. try {
  9. const res = await serverGraphqlFetch<LogoutData>({
  10. query: CUSTOMER_LOGOUT,
  11. });
  12. let success = true;
  13. let msg = "";
  14. if(res.error) {
  15. success = false;
  16. msg = res.error.message;
  17. } else {
  18. success = res.data?.createLogout?.logout?.success ?? false;
  19. msg = res.data?.createLogout?.logout?.message ?? "";
  20. if(success) {
  21. const cookieStore = await cookies();
  22. // 设置游客状态
  23. cookieStore.set(IS_GUEST, "true");
  24. }
  25. }
  26. return {
  27. success,
  28. message: msg,
  29. };
  30. } catch (error: unknown) {
  31. return {
  32. success: false,
  33. message: error instanceof Error ? error.message : "Something went wrong",
  34. };
  35. }
  36. }