Skip to content

Commit

Permalink
토큰에 추가된 userId를 반영하여 로그인 성공 시 유저 고유의 마이페이지로 리다이렉트 한다. (SWYP-team-2th#131
Browse files Browse the repository at this point in the history
)
  • Loading branch information
YOOJS1205 committed Feb 28, 2025
1 parent 996fc43 commit bd9095c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
3 changes: 2 additions & 1 deletion src/api/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
removeAccessToken,
} from '@/components/login/Auth/token';

const isDevelopment = window.location.host.includes('dev.');
const isDevelopment =
import.meta.env.VITE_API_URL_DEV || window.location.host.includes('dev.');

const axiosConfig: AxiosRequestConfig = {
baseURL: isDevelopment
Expand Down
27 changes: 27 additions & 0 deletions src/api/useGetIdentificationInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useQuery } from '@tanstack/react-query';
import { request } from '@/api/config';
import { getAccessToken } from '@/components/login/Auth/token';

interface IdentificationInfoResponse {
id: number;
nickname: string;
profileUrl: string;
}

export default function useGetIdentificationInfo() {
return useQuery<IdentificationInfoResponse>({
queryKey: ['identificationInfo'],
queryFn: async () => {
const token = getAccessToken();
return request<IdentificationInfoResponse>({
method: 'GET',
url: '/users/me',
headers: {
Authorization: `Bearer ${token}`,
},
});
},
// 토큰이 저장되도 여기 쿼리문이 실행되지 않기 때문에 토큰이 들어오면 현재 쿼리문 실행되도록 설정
enabled: !!getAccessToken(),
});
}
8 changes: 6 additions & 2 deletions src/api/usePostKaKaoLogin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { useMutation } from '@tanstack/react-query';
import { useNavigate } from 'react-router-dom';
import { request } from '@/api/config';
import { setAccessToken } from '@/components/login/Auth/token';

interface LoginResponseType {
accessToken: string;
userId: number;
}

interface LoginRequestType {
Expand All @@ -12,21 +14,23 @@ interface LoginRequestType {
}

export default function usePostKakaoLogin() {
const navigate = useNavigate();

return useMutation<LoginResponseType, Error, LoginRequestType>({
mutationFn: async (data) => {
console.log('API 요청 데이터:', data);
return request<LoginResponseType>({
method: 'POST',
url: '/auth/oauth2/code/kakao',
data,
});
},
onSuccess: (data) => {
console.log('로그인 성공, 토큰:', data.accessToken);
setAccessToken(data.accessToken);
navigate(`/user/${data.userId}`);
},
onError: (err) => {
console.error('로그인 실패:', err);
navigate('/onboarding');
},
});
}
23 changes: 4 additions & 19 deletions src/pages/Login/OAuthPage.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
import { useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import usePostKakaoLogin from '@/api/usePostKaKaoLogin';

export default function OAuthPage() {
const navigate = useNavigate();
const { mutate } = usePostKakaoLogin();

useEffect(() => {
const code = new URL(window.location.href).searchParams.get('code');

if (code) {
mutate(
{
redirectUri: `${window.location.origin}/oauth`,
code,
},
{
onSuccess: (data) => {
console.log('로그인 성공, 토큰:', data.accessToken);
// 추후 onboarding -> 마이페이지 userId 포함해서 수정 예정
navigate('/onboarding');
},
onError: (err) => {
console.error('로그인 실패:', err);
navigate('/onboarding');
},
},
);
mutate({
redirectUri: `${window.location.origin}/oauth`,
code,
});
}
}, []);

Expand Down

0 comments on commit bd9095c

Please sign in to comment.