File tree Expand file tree Collapse file tree 3 files changed +40
-1
lines changed
Expand file tree Collapse file tree 3 files changed +40
-1
lines changed Original file line number Diff line number Diff line change @@ -5,11 +5,16 @@ import Link from "next/link";
55import SubmitButton from "./SubMitButton" ;
66import { useRouter } from "next/router" ;
77import useAuthStore from "@/store/useAuthStore" ;
8+ import { useEffect } from "react" ;
89
910const 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 ? (
Original file line number Diff line number Diff line change 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 ;
Original file line number Diff line number Diff line change @@ -8,10 +8,12 @@ import {
88 postEasySignIn ,
99} from "@/lib/api/auth" ;
1010import { getUserInfo } from "@/lib/api/user" ;
11+ import { proxy } from "@/lib/api/axiosInstanceApi" ;
1112
1213interface 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 ;
You can’t perform that action at this time.
0 commit comments