Skip to content

Commit

Permalink
refactor: 인증시 로그인 유저 정보가 없으면 닉네임 설정 페이지로 전환 되도록 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
bytrustu committed Jul 8, 2023
1 parent dbd4b9d commit e2a4240
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions apps/client/src/components/login/page/AuthPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,44 @@

import { useEffect } from 'react';
import { useRouter, useSearchParams } from 'next/navigation';
import { ACCESS_TOKEN_KEY, REFRESH_TOKEN_KEY, setLocalStorageItem } from '@oseek/lib';

const REDIRECT_URL = {
MAIN: '/',
SETTING_NAME: '/login/setting-name',
};

const PARAMS = {
ACCESS_TOKEN: 'accessToken',
REFRESH_TOKEN: 'refreshToken',
IS_FIRST_LOGIN: 'isFirstLogin',
};
import { ACCESS_TOKEN_KEY, REFRESH_TOKEN_KEY, getLocalStorageItem, setLocalStorageItem, USER_KEY } from '@oseek/lib';
import { memberApi } from '@oseek/apis';
import { PAGE_URL } from '@constant/pageUrl';

export const AuthPage = () => {
const router = useRouter();
const searchParams = useSearchParams();

const accessToken = searchParams.get(PARAMS.ACCESS_TOKEN);
const refreshToken = searchParams.get(PARAMS.REFRESH_TOKEN);
const isFirstLogin = searchParams.get(PARAMS.IS_FIRST_LOGIN);
const handleTokens = (accessTokenQuery: string | null, refreshTokenQuery: string | null) => {
if (accessTokenQuery && refreshTokenQuery) {
setLocalStorageItem(ACCESS_TOKEN_KEY, accessTokenQuery);
setLocalStorageItem(REFRESH_TOKEN_KEY, refreshTokenQuery);
return true;
}
return false;
};

const fetchAndRedirect = async () => {
const {
data: { nickname, location },
} = await memberApi.findMemberInfoAxios();

if (nickname) {
setLocalStorageItem(USER_KEY, { nickname, location });
router.replace(PAGE_URL.MAIN);
return;
}
router.replace(PAGE_URL.SETTING_NAME);
};

useEffect(() => {
if (accessToken && refreshToken) {
setLocalStorageItem(ACCESS_TOKEN_KEY, accessToken);
setLocalStorageItem(REFRESH_TOKEN_KEY, refreshToken);
if (isFirstLogin) {
router.replace(REDIRECT_URL.SETTING_NAME);
return;
}
router.push(REDIRECT_URL.MAIN);
const accessTokenQuery = searchParams.get('accessToken');
const refreshTokenQuery = searchParams.get('refreshToken');

if (handleTokens(accessTokenQuery, refreshTokenQuery)) {
fetchAndRedirect();
}
}, [accessToken, refreshToken]);
}, [router, searchParams]);

return <div></div>;
return <div />;
};

0 comments on commit e2a4240

Please sign in to comment.