Skip to content

Commit 16935a2

Browse files
authored
Merge pull request #68 from codeit9-temporary/feature/AuthStore
Fix:AuthStore checkLogin 액션 추가
2 parents fdb3c5b + 2b60bab commit 16935a2

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

components/HeaderMenu.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ import Link from "next/link";
55
import SubmitButton from "./SubMitButton";
66
import { useRouter } from "next/router";
77
import useAuthStore from "@/store/useAuthStore";
8+
import { useEffect } from "react";
89

910
const HeaderMenu = () => {
10-
const { user, isLoggedIn } = useAuthStore();
11+
const { user, isLoggedIn, checkLogin } = useAuthStore();
1112
const router = useRouter();
1213

14+
useEffect(() => {
15+
checkLogin();
16+
}, [checkLogin]);
17+
1318
return (
1419
<>
1520
{!isLoggedIn ? (

pages/api/auth/check.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { NextApiRequest, NextApiResponse } from "next";
2+
import { parse } from "cookie";
3+
4+
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
5+
const cookies = req.headers.cookie ? parse(req.headers.cookie) : {};
6+
const token = cookies.accessToken;
7+
8+
if (token) {
9+
return res.status(200).json({ isLoggedIn: true });
10+
}
11+
12+
return res.status(200).json({ isLoggedIn: false });
13+
};
14+
15+
export default handler;

store/useAuthStore.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import {
88
postEasySignIn,
99
} from "@/lib/api/auth";
1010
import { getUserInfo } from "@/lib/api/user";
11+
import { proxy } from "@/lib/api/axiosInstanceApi";
1112

1213
interface AuthStore {
1314
user: User | null;
1415
isLoggedIn: boolean;
16+
checkLogin: () => Promise<void>;
1517
login: (body: signInProps) => Promise<boolean>;
1618
SNSLogin: (
1719
provider: "google" | "kakao",
@@ -26,6 +28,23 @@ const useAuthStore = create<AuthStore>()(
2628
user: null,
2729
isLoggedIn: false,
2830

31+
checkLogin: async () => {
32+
try {
33+
const response = await proxy.get("/api/auth/check");
34+
if (response.data.isLoggedIn) {
35+
const userInfo = await getUserInfo(); // 로그인 되어 있으면 사용자 정보 가져오기
36+
if (userInfo) {
37+
set({ isLoggedIn: true, user: userInfo });
38+
}
39+
} else {
40+
set({ isLoggedIn: false, user: null });
41+
}
42+
} catch (error) {
43+
console.error("로그인 상태 확인 중 오류 발생", error);
44+
set({ isLoggedIn: false, user: null });
45+
}
46+
},
47+
2948
login: async (body) => {
3049
try {
3150
const { email, password } = body;

0 commit comments

Comments
 (0)