Skip to content

Commit

Permalink
Feat: #107 로그인 시 유저 아이디 전역 관리
Browse files Browse the repository at this point in the history
  • Loading branch information
cxaosdev committed Dec 3, 2024
1 parent a05a031 commit 3e2011a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
17 changes: 14 additions & 3 deletions src/api/auth.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import axios from "axios";

export async function requestSocialLogin(provider, accessToken) {
// 소셜 로그인 요청
export async function requestSocialLogin(platform, code) {
const response = await axios.post("/api/auth/social-login", {
provider: provider,
access_token: accessToken,
provider: platform,
access_token: code,
});
return response.data;
}

// 사용자 정보 조회
export async function fetchUserInfo(token) {
const response = await axios.get(`/api/users/me`, {
headers: {
Authorization: `Bearer ${token}`,
},
});
return response.data;
}
14 changes: 8 additions & 6 deletions src/context/AuthContext.jsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import React, { createContext, useContext, useState } from "react";

// AuthContext 생성
const AuthContext = createContext();

// Context Provider Component 생성
export function AuthProvider({ children }) {
const [isLoggedIn, setLoggedIn] = useState(false);
const [userInfo, setUserInfo] = useState(null);
const [loggedIn, setLoggedIn] = useState(false);
const [userInfo, setUserInfo] = useState(null); // 사용자 정보 전역 관리

return (
<AuthContext.Provider
value={{ isLoggedIn, setLoggedIn, userInfo, setUserInfo }}
value={{
loggedIn,
setLoggedIn,
userInfo,
setUserInfo,
}}
>
{children}
</AuthContext.Provider>
);
}

// Context 커스텀 훅 생성
export function useAuth() {
return useContext(AuthContext);
}
28 changes: 12 additions & 16 deletions src/pages/LoginPostCode.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React, { useEffect, useState } from "react";
import { useNavigate, useSearchParams } from "react-router-dom";
import { requestSocialLogin } from "../api/auth";
import { fetchUserInfo, requestSocialLogin } from "../api/auth";
import { useAuth } from "../context/AuthContext";

export default function LoginPostCode() {
const [searchParams] = useSearchParams();
const [errorMessage, setErrorMessage] = useState("");
const navigate = useNavigate();
const { setLoggedIn, setUserInfo } = useAuth(); // useAuth 사용하여 상태 함수 가져오기
const { setLoggedIn, setUserInfo } = useAuth();

useEffect(() => {
const authCode = searchParams.get("code");
Expand All @@ -20,24 +20,20 @@ export default function LoginPostCode() {
}

try {
console.log(
"Authenticating for platform:",
platform,
"code:",
authCode,
);

// 로그인 요청
const response = await requestSocialLogin(platform, authCode);
console.log("Authentication success:", response);
const { token } = response;

// 로컬 스토리지에 토큰 저장
localStorage.setItem("token", response.token);
// 토큰 저장
localStorage.setItem("token", token);

// 사용자 정보 상태 설정
// 사용자 정보
const userInfo = await fetchUserInfo(token);
console.log("User ID:", userInfo.user_id);
// 전역 상태 업데이트
setLoggedIn(true);
setUserInfo({ ...response.userInfo, token: response.token });

// 홈 화면으로 이동
setUserInfo(userInfo);
// 캘린더 화면으로 이동
navigate("/calendar");
} catch (error) {
setErrorMessage("인증에 실패했습니다. 다시 시도해 주세요.");
Expand Down

0 comments on commit 3e2011a

Please sign in to comment.