Skip to content

Commit

Permalink
Merge pull request #228 from epigram5-9/refactor/FE-29
Browse files Browse the repository at this point in the history
FE-29 ♻️ 로그인 페이지 리팩토링
  • Loading branch information
jangmoonwon authored Aug 7, 2024
2 parents c13d755 + ab20548 commit d82d58c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 23 deletions.
11 changes: 0 additions & 11 deletions src/apis/oauth.ts

This file was deleted.

11 changes: 11 additions & 0 deletions src/apis/postKakaoOauth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import httpClient from '.';

const postKakaoOauth = async (code: string) => {
const response = await httpClient.post('/auth/signIn/KAKAO', {
redirectUri: process.env.NEXT_PUBLIC_REDIRECT_URI,
token: code,
});
return response.data;
};

export default postKakaoOauth;
4 changes: 2 additions & 2 deletions src/hooks/useKakaoLogin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import postOauth from '@/apis/oauth';
import postKakaoOauth from '@/apis/postKakaoOauth';
import { toast } from '@/components/ui/use-toast';
import { useMutation } from '@tanstack/react-query';
import { isAxiosError } from 'axios';
Expand All @@ -9,7 +9,7 @@ const useKakaoLogin = () => {

return useMutation({
mutationFn: async (code: string) => {
const result = await postOauth(code);
const result = await postKakaoOauth(code);
localStorage.setItem('accessToken', result.accessToken);
localStorage.setItem('refreshToken', result.refreshToken);
return result;
Expand Down
3 changes: 1 addition & 2 deletions src/hooks/useRegisterMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ const useRegisterMutation = (onRegisterError: (field: 'email' | 'nickname') => v
onSuccess: (data) => {
localStorage.setItem('accessToken', data.accessToken);
localStorage.setItem('refreshToken', data.refreshToken);
router.push('/auth/SignIn');
router.push('/epigrams');
toast({
title: '회원가입 성공!',
description: '로그인 후 이용해주세요.',
className: 'bg-illust-green text-white font-semibold',
});
},
Expand Down
17 changes: 13 additions & 4 deletions src/pages/auth/SignIn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,26 @@ import useRefreshToken from '@/hooks/useRefreshToken';

export default function SignIn() {
const mutationSignin = useSigninMutation();
const { mutate: refreshAccessToken } = useRefreshToken();
const { mutateAsync: refreshAccessToken } = useRefreshToken();
const router = useRouter();

useEffect(() => {
const refreshToken = typeof window !== 'undefined' ? localStorage.getItem('refreshToken') : null;

if (refreshToken) {
refreshAccessToken({ refreshToken });
router.push('/epigrams');
const handleTokenRefresh = async () => {
try {
await refreshAccessToken({ refreshToken });
router.push('/epigrams'); // 토큰 갱신 후 리디렉션
} catch {
localStorage.removeItem('refreshToken');
router.push('/auth/SignIn');
}
};

handleTokenRefresh();
}
}, [refreshAccessToken]);
}, [refreshAccessToken, router]);

// 폼 정의
const form = useForm<PostSigninRequestType>({
Expand Down
17 changes: 13 additions & 4 deletions src/pages/auth/SignUp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,26 @@ import useRefreshToken from '@/hooks/useRefreshToken';

export default function SignUp() {
const [focusedField, setFocusedField] = useState<string | null>(null);
const { mutate: refreshAccessToken } = useRefreshToken();
const { mutateAsync: refreshAccessToken } = useRefreshToken();
const router = useRouter();

useEffect(() => {
const refreshToken = typeof window !== 'undefined' ? localStorage.getItem('refreshToken') : null;

if (refreshToken) {
refreshAccessToken({ refreshToken });
router.push('/epigrams');
const handleTokenRefresh = async () => {
try {
await refreshAccessToken({ refreshToken });
router.push('/epigrams'); // 토큰 갱신 후 리디렉션
} catch {
localStorage.removeItem('refreshToken');
router.push('/auth/SignIn');
}
};

handleTokenRefresh();
}
}, [refreshAccessToken]);
}, [refreshAccessToken, router]);

const form = useForm<PostSignUpRequestType>({
resolver: zodResolver(PostSignUpRequest),
Expand Down

0 comments on commit d82d58c

Please sign in to comment.