Skip to content

Commit

Permalink
fix: 로그인 되지 않던 이슈 해결 (#432)
Browse files Browse the repository at this point in the history
* refactor: 렌더링 이후에 api 호출하도록 변경

* refactor: 로그인 했을 경우 Success, Error callback 작성
  • Loading branch information
hozzijeong committed Oct 20, 2023
1 parent 0797117 commit f2a80f9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
20 changes: 18 additions & 2 deletions frontend/src/hooks/queries/auth/useLogin.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
import { useMutation } from '@tanstack/react-query';
import { useNavigate } from 'react-router-dom';
import useAddToast from 'hooks/@common/useAddToast';
import AuthAPI from 'apis/auth';
import throwOnInvalidStatus from 'utils/throwOnInvalidStatus';
import { URL_PATH } from 'constants/index';

const useLogin = () =>
useMutation<null, Error, string>({
const useLogin = () => {
const navigation = useNavigate();
const addToast = useAddToast();

return useMutation<null, Error, string>({
mutationFn: async (code: string) => {
const response = await AuthAPI.getSessionId(code);

throwOnInvalidStatus(response);
return null;
},

onSuccess: () => {
navigation(URL_PATH.reminder, { replace: true });
},

onError: (error) => {
addToast({ type: 'error', message: error.message, time: 3000 });
navigation(URL_PATH.login, { replace: true });
},
});
};

export default useLogin;
7 changes: 4 additions & 3 deletions frontend/src/pages/auth/Login/Authorization.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { useEffect } from 'react';
import { Navigate, useSearchParams } from 'react-router-dom';
import { useSearchParams } from 'react-router-dom';
import Loading from 'pages/@common/Loading';
import useLogin from 'hooks/queries/auth/useLogin';
import { URL_PATH } from 'constants/index';

const Authorization = () => {
const [searchParams] = useSearchParams();
const code = searchParams.get('code');

const { mutate: login } = useLogin();

useEffect(() => {
login(code ?? '');
}, [code, login]);

return <Navigate to={URL_PATH.reminder} replace />;
return <Loading />;
};

export default Authorization;

0 comments on commit f2a80f9

Please sign in to comment.