Skip to content

Commit 97a080e

Browse files
committed
feat: added redirecting if routing to auth routes
1 parent 884fe46 commit 97a080e

3 files changed

Lines changed: 26 additions & 22 deletions

File tree

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
11
"use client";
22

3-
import React, { useEffect } from "react";
3+
import React, { useEffect, useState } from "react";
4+
import { useRouter, usePathname } from "next/navigation";
5+
import { useMerchantHydrated, useMerchantSession } from "@/lib/merchant-store";
46

57
export default function AuthGuard({ children }: { children: React.ReactNode }) {
8+
const hydrated = useMerchantHydrated();
9+
const session = useMerchantSession();
10+
const router = useRouter();
11+
const pathname = usePathname();
12+
const [mounted, setMounted] = useState(false);
13+
614
useEffect(() => {
7-
// Component mounted
15+
setMounted(true);
816
}, []);
917

10-
// useEffect(() => {
11-
// const isBypass = typeof window !== "undefined" &&
12-
// (window.location.search.includes("bypass=true") || process.env.NEXT_PUBLIC_DEV_BYPASS === "true");
13-
14-
// if (mounted && hydrated && !session && !isBypass) {
15-
// router.push(`/login?callbackUrl=${encodeURIComponent(pathname)}`);
16-
// }
17-
// }, [mounted, hydrated, session, router, pathname]);
18-
19-
// if (!mounted || !hydrated) {
20-
// return null;
21-
// }
22-
23-
// const isBypass = typeof window !== "undefined" &&
24-
// (window.location.search.includes("bypass=true") || process.env.NEXT_PUBLIC_DEV_BYPASS === "true");
18+
useEffect(() => {
19+
if (mounted && hydrated && !session) {
20+
router.push(`/login?callbackUrl=${encodeURIComponent(pathname)}`);
21+
}
22+
}, [mounted, hydrated, session, router, pathname]);
2523

26-
// if (!session && !isBypass) {
27-
// return null;
28-
// }
24+
if (!mounted || !hydrated) return null;
25+
if (!session) return null;
2926

3027
return <>{children}</>;
3128
}

frontend/src/components/MerchantProfileCard.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default function MerchantProfileCard() {
4242
const handleLogout = () => {
4343
logout();
4444
setShowDropdown(false);
45-
window.location.href = "/";
45+
window.location.href = "/login";
4646
};
4747

4848
return (

frontend/src/lib/merchant-store.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,15 @@ export const useMerchantStore = create<MerchantStore>((set) => ({
231231
logout: () => {
232232
if (typeof window !== "undefined") {
233233
localStorage.removeItem(TOKEN_KEY);
234+
localStorage.removeItem(API_KEY_KEY);
235+
localStorage.removeItem(MERCHANT_KEY);
234236
}
235-
set({ token: null, session: null });
237+
set({
238+
token: null,
239+
session: null,
240+
apiKey: null,
241+
merchant: null,
242+
});
236243
},
237244
}));
238245

0 commit comments

Comments
 (0)