Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FE-29 ♻️ 로그인 페이지 리팩토링 #228

Merged
merged 6 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading