Skip to content

Commit

Permalink
Merge pull request #211 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 6, 2024
2 parents 29cc9a3 + a29dcb9 commit 836580d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/apis/auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PostSigninRequestType, PostSigninResponseType, PostSignUpRequestType, PostSignUpResponseType } from '@/schema/auth';
import type { PostRefreshTokenRequestType, PostRefreshTokenResponseType, PostSigninRequestType, PostSigninResponseType, PostSignUpRequestType, PostSignUpResponseType } from '@/schema/auth';
import httpClient from '.';

export const postSignin = async (request: PostSigninRequestType): Promise<PostSigninResponseType> => {
Expand All @@ -10,3 +10,8 @@ export const postSignup = async (request: PostSignUpRequestType): Promise<PostSi
const response = await httpClient.post('/auth/signUp', request);
return response.data;
};

export const postRefreshToken = async (request: PostRefreshTokenRequestType): Promise<PostRefreshTokenResponseType> => {
const response = await httpClient.post('/auth/refresh-token', request);
return response.data;
};
16 changes: 16 additions & 0 deletions src/hooks/useRefreshToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useMutation } from '@tanstack/react-query';
import { postRefreshToken } from '@/apis/auth';
import { PostRefreshTokenRequestType, PostRefreshTokenResponseType } from '@/schema/auth';

const useRefreshToken = () =>
useMutation<PostRefreshTokenResponseType, Error, PostRefreshTokenRequestType>({
mutationFn: postRefreshToken,
onSuccess: (data) => {
localStorage.setItem('accessToken', data.accessToken);
},
onError: () => {
localStorage.removeItem('refreshToken');
},
});

export default useRefreshToken;
10 changes: 7 additions & 3 deletions src/pages/auth/SignIn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ import { zodResolver } from '@hookform/resolvers/zod';
import { Form, FormControl, FormField, FormItem, FormMessage } from '@/components/ui/form';
import { PostSigninRequest, PostSigninRequestType } from '@/schema/auth';
import useSigninMutation from '@/hooks/useSignInMutation';
import useRefreshToken from '@/hooks/useRefreshToken';

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

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

if (refreshToken) {
refreshAccessToken({ refreshToken });
router.push('/epigrams');
}
}, [router]);
}, [refreshAccessToken]);

// 폼 정의
const form = useForm<PostSigninRequestType>({
Expand Down
10 changes: 7 additions & 3 deletions src/pages/auth/SignUp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ import { Input } from '@/components/ui/input';
import { Button } from '@/components/ui/button';
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form';
import useRegisterMutation from '@/hooks/useRegisterMutation';
import useRefreshToken from '@/hooks/useRefreshToken';

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

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

if (refreshToken) {
refreshAccessToken({ refreshToken });
router.push('/epigrams');
}
}, [router]);
}, [refreshAccessToken]);

const form = useForm<PostSignUpRequestType>({
resolver: zodResolver(PostSignUpRequest),
Expand Down
11 changes: 11 additions & 0 deletions src/schema/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,20 @@ export const PostAuthResponse = z.object({
user: User,
});

export const PostRefreshTokenRequest = z.object({
refreshToken: z.string(),
});

export const PostRefreshTokenResponse = z.object({
accessToken: z.string(),
});

// NOTE: 회원가입 타입
export type PostSignUpRequestType = z.infer<typeof PostSignUpRequest>;
export type PostSignUpResponseType = z.infer<typeof PostAuthResponse>;
// NOTE: 로그인 타입
export type PostSigninRequestType = z.infer<typeof PostSigninRequest>;
export type PostSigninResponseType = z.infer<typeof PostAuthResponse>;
// NOTE: 리프레시 토큰
export type PostRefreshTokenRequestType = z.infer<typeof PostRefreshTokenRequest>;
export type PostRefreshTokenResponseType = z.infer<typeof PostRefreshTokenResponse>;

0 comments on commit 836580d

Please sign in to comment.