From a5a09b634e42821e8f4c0916693482e2190b3275 Mon Sep 17 00:00:00 2001 From: kr-nius Date: Wed, 13 Mar 2024 17:11:05 +0900 Subject: [PATCH] =?UTF-8?q?[fix]=20#36=20=EC=BD=94=EB=93=9C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Components/Molecules/LoginForm.tsx | 6 +- .../src/Store/AuthUserEmail.ts | 22 ++++++ .../src/app/mypage/page.tsx | 79 ++++++++++--------- 3 files changed, 68 insertions(+), 39 deletions(-) create mode 100644 weatherfit_refactoring/src/Store/AuthUserEmail.ts diff --git a/weatherfit_refactoring/src/Components/Molecules/LoginForm.tsx b/weatherfit_refactoring/src/Components/Molecules/LoginForm.tsx index 834261e..c56244f 100644 --- a/weatherfit_refactoring/src/Components/Molecules/LoginForm.tsx +++ b/weatherfit_refactoring/src/Components/Molecules/LoginForm.tsx @@ -5,10 +5,12 @@ import { confirmAlert } from '@/utils/function/utilFunction' import { useRouter } from 'next/navigation' import { FormEvent, useState } from 'react' import { useAuth } from '../../../contexts/AuthContext' +import AuthUserEmailStore from '@/Store/AuthUserEmail' export default function LoginForm() { const [email, setEmail] = useState('') const [password, setPassword] = useState('') + const { userEmail, setUserEmail } = AuthUserEmailStore() const router = useRouter() const { login } = useAuth() @@ -34,7 +36,9 @@ export default function LoginForm() { const loginRes = await res.json() console.log('로그인 loginRes: ', loginRes) - login(loginRes.email, loginRes.token) + setUserEmail(loginRes.email) + + // login(loginRes.email, loginRes.token) confirmAlert(`${loginRes.nickname}님 환영합니다!`) diff --git a/weatherfit_refactoring/src/Store/AuthUserEmail.ts b/weatherfit_refactoring/src/Store/AuthUserEmail.ts new file mode 100644 index 0000000..ff591bc --- /dev/null +++ b/weatherfit_refactoring/src/Store/AuthUserEmail.ts @@ -0,0 +1,22 @@ +import { create } from 'zustand' +import { persist } from 'zustand/middleware' + +interface UserEmailStore { + userEmail: string | null + setUserEmail: (userEmail: string | null) => void +} + +const AuthUserEmailStore = create( + persist( + (set, get) => ({ + userEmail: null, + setUserEmail: (userEmail: string | null) => + set(() => ({ userEmail: userEmail })), + }), + { + name: 'user_email', + }, + ), +) + +export default AuthUserEmailStore diff --git a/weatherfit_refactoring/src/app/mypage/page.tsx b/weatherfit_refactoring/src/app/mypage/page.tsx index eab5312..51f83db 100644 --- a/weatherfit_refactoring/src/app/mypage/page.tsx +++ b/weatherfit_refactoring/src/app/mypage/page.tsx @@ -4,6 +4,7 @@ import React, { useEffect, useState } from 'react' import ProfileInfo from '@/Components/Molecules/ProfileInfo' import ProfileHeader from '@/Components/Organisms/ProfileHeader' import ProfileBoard from '@/Components/Organisms/ProfileBoard' +import AuthUserEmailStore from '@/Store/AuthUserEmail' export default function Mypage() { // 회원 정보 @@ -12,53 +13,55 @@ export default function Mypage() { const [refreshProfile, setRefreshProfile] = useState(false) // 회원 정보 변경했을 때 const [myPostData, setMyPostData] = useState([]) const [myLikePostData, setMyLikePostData] = useState([]) + const { userEmail, setUserEmail } = AuthUserEmailStore() + console.log('user email: ', userEmail) // 회원 정보 불러오기 - useEffect(() => { - const fetchData = async () => { - try { - // 프로필 데이터 가져오기 - const res = await fetch(`https://www.jerneithe.site/user/api/profile`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - Authorization: 'Bearer', - }, - body: JSON.stringify({ - email: localStorage.getItem('user_email'), - }), - }) + // useEffect(() => { + // const fetchData = async () => { + // try { + // // 프로필 데이터 가져오기 + // const res = await fetch(`https://www.jerneithe.site/user/api/profile`, { + // method: 'POST', + // headers: { + // 'Content-Type': 'application/json', + // Authorization: 'Bearer', + // }, + // body: JSON.stringify({ + // email: localStorage.getItem('user_email'), + // }), + // }) - const profileRes = await res.json() + // const profileRes = await res.json() - setUserProfile(profileRes) - setUserImage(profileRes.image) + // setUserProfile(profileRes) + // setUserImage(profileRes.image) - console.log('유저 data: ', profileRes) + // console.log('유저 data: ', profileRes) - // ----------------------------------------- + // // ----------------------------------------- - // 게시물 데이터 가져오기 - const boardListRes = await fetch( - 'https://www.jerneithe.site/board/list', - ) - const boardListdata: FEEDDATA[] = await boardListRes.json() + // // 게시물 데이터 가져오기 + // const boardListRes = await fetch( + // 'https://www.jerneithe.site/board/list', + // ) + // const boardListdata: FEEDDATA[] = await boardListRes.json() - const filteredData = boardListdata.filter( - item => item.nickName === userPofile.nickname, - ) - setMyPostData(filteredData) + // const filteredData = boardListdata.filter( + // item => item.nickName === userPofile.nickname, + // ) + // setMyPostData(filteredData) - const filteredLikeData = boardListdata.filter(item => - item.likelist.some(like => like.nickName === userPofile.nickname), - ) - setMyLikePostData(filteredLikeData) - } catch (error) { - console.error('데이터 로딩 에러: ', error) - } - } - fetchData() - }, [refreshProfile]) + // const filteredLikeData = boardListdata.filter(item => + // item.likelist.some(like => like.nickName === userPofile.nickname), + // ) + // setMyLikePostData(filteredLikeData) + // } catch (error) { + // console.error('데이터 로딩 에러: ', error) + // } + // } + // fetchData() + // }, [refreshProfile]) return ( <>