Skip to content

Commit 0652041

Browse files
committed
add persistent store for auth
1 parent b6694c7 commit 0652041

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

peerprep-fe/src/app/signin/page.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
99
import { axiosUserClient } from '@/network/axiosClient';
1010
import { login } from '@/lib/auth';
1111
import { useRouter } from 'next/navigation';
12+
import { useAuthStore } from '@/state/useAuthStore';
1213

1314
export default function LoginForm() {
1415
const [email, setEmail] = useState('');
1516
const [password, setPassword] = useState('');
1617
const [error, setError] = useState('');
1718
const router = useRouter();
19+
const setAuth = useAuthStore((state) => state.setAuth);
1820

1921
// handle login here
2022
const handleLogin = async (e: React.FormEvent) => {
@@ -31,6 +33,7 @@ export default function LoginForm() {
3133
const token = data.accessToken;
3234
const res = await login(token);
3335
if (res) {
36+
setAuth(true, token);
3437
router.push('/');
3538
return;
3639
}

peerprep-fe/src/components/navbar/Navbar.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ import {
1010
DropdownMenuItem,
1111
DropdownMenuTrigger,
1212
} from '@/components/ui/dropdown-menu';
13-
import { useRouter } from 'next/navigation';
13+
import { useAuthStore } from '@/state/useAuthStore';
1414

1515
export default function Navbar() {
16-
const router = useRouter();
16+
const { isAuth, clearAuth } = useAuthStore();
1717

1818
const handleLogout = async () => {
1919
const res = await logout();
2020
if (res) {
21-
router.push('/signin');
21+
clearAuth();
2222
return;
2323
}
2424
};
@@ -36,7 +36,7 @@ export default function Navbar() {
3636
<Link href="/match" className="text-gray-300 hover:text-white">
3737
Match
3838
</Link>
39-
{true ? (
39+
{isAuth ? (
4040
<DropdownMenu>
4141
<DropdownMenuTrigger asChild>
4242
<Button
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { create } from 'zustand';
2+
import { persist, createJSONStorage } from 'zustand/middleware';
3+
4+
// Define the store interface
5+
interface AuthState {
6+
isAuth: boolean;
7+
token: string | null;
8+
setAuth: (isAuth: boolean, token: string | null) => void;
9+
clearAuth: () => void;
10+
}
11+
12+
// Create the persistent store with JSON storage
13+
export const useAuthStore = create<AuthState>()(
14+
persist(
15+
(set) => ({
16+
isAuth: false,
17+
token: null,
18+
setAuth: (isAuth: boolean, token: string | null) =>
19+
set({ isAuth, token }),
20+
clearAuth: () => set({ isAuth: false, token: null }),
21+
}),
22+
{
23+
name: 'auth-storage',
24+
storage: createJSONStorage(() => localStorage),
25+
},
26+
),
27+
);

0 commit comments

Comments
 (0)