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

Gallery, 토큰 관리 로직 병합 #493

Merged
merged 9 commits into from
Mar 20, 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
Binary file not shown.
Binary file not shown.
Binary file not shown.
13 changes: 13 additions & 0 deletions @types/next-auth.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import NextAuth, { DefaultSession } from 'next-auth';

declare module 'next-auth' {
/**
* Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
*/
interface Session {
user: {
accessToken: string;
tokenType: string;
} & DefaultSession['user'];
}
}
47 changes: 0 additions & 47 deletions src/components/auth/AuthProvider.tsx

This file was deleted.

26 changes: 26 additions & 0 deletions src/components/auth/AuthSessionLoader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { type ReactNode, useLayoutEffect } from 'react';
import { useSession } from 'next-auth/react';
import { useSetAtom } from 'jotai';

import { LOCAL_STORAGE_KEY } from '~/constants/storage';
import { isUserTokenValidAtom } from '~/store/auth';

interface Props {
children: ReactNode;
}

const AuthSessionLoader = ({ children }: Props) => {
const { data, status } = useSession();
const setIsUserTokenValid = useSetAtom(isUserTokenValidAtom);

useLayoutEffect(() => {
if (status !== 'authenticated') return;

setIsUserTokenValid(true);
localStorage.setItem(LOCAL_STORAGE_KEY.accessToken, data.user.accessToken);
}, [data, setIsUserTokenValid, status]);

return <>{children}</>;
};

export default AuthSessionLoader;
2 changes: 1 addition & 1 deletion src/components/sideMenu/MenuSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function MenuSection() {
label: '로그아웃',
action: () => {
// 실 환경에서 되는지 체크
logOutHandler();
logOutHandler({ callbackUrl: '/' });
router.replace('/');
},
},
Expand Down
4 changes: 2 additions & 2 deletions src/features/home/KakaoLoginButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const KakaoLoginButton = () => {
if (status === 'authenticated') {
return (
<div css={KakaoLoginWrapper}>
<button type="button" onClick={logOutHandler}>
<button type="button" onClick={() => logOutHandler()}>
로그아웃
</button>
</div>
Expand All @@ -23,7 +23,7 @@ const KakaoLoginButton = () => {
return (
<div css={KakaoLoginWrapper}>
이미 질문폼이 있다면?
<button type="button" css={KakaoLoginButtonCss} onClick={loginHandler}>
<button type="button" css={KakaoLoginButtonCss} onClick={() => loginHandler({ callbackUrl: '/gallery' })}>
로그인하고 결과 보기
</button>
</div>
Expand Down
10 changes: 4 additions & 6 deletions src/hooks/auth/useKakaoLogin.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { signIn, signOut, useSession } from 'next-auth/react';
import { signIn, type SignInOptions, signOut, type SignOutParams, useSession } from 'next-auth/react';

import { LOCAL_STORAGE_KEY } from '~/constants/storage';

const useKakaoLogin = () => {
const { status } = useSession();

const logOutHandler = () => {
signOut();
const logOutHandler = (options?: SignOutParams) => {
localStorage.removeItem(LOCAL_STORAGE_KEY.accessToken);
signOut(options);
};

const loginHandler = () => {
signIn('kakao');
};
const loginHandler = (options?: SignInOptions) => signIn('kakao', options);

return {
logOutHandler,
Expand Down
32 changes: 16 additions & 16 deletions src/pages/_app.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Analytics } from '@vercel/analytics/react';
import { domMax, LazyMotion } from 'framer-motion';
import { useOpenExternalBrowser } from 'open-external-browser';

import AuthProvider from '~/components/auth/AuthProvider';
import AuthSessionLoader from '~/components/auth/AuthSessionLoader';
import ErrorBoundary from '~/components/error/ErrorBoundary';
import MonitoringInitializer from '~/components/monitoring/MonitoringInitializer';
import NewFeedbackSnackBarListener from '~/components/snackBar/NewFeedbackSnackBarListener';
Expand Down Expand Up @@ -55,14 +55,14 @@ export default function App({ Component, pageProps }: AppPropsWithLayout) {

return (
<SessionProvider session={pageProps.session}>
<MonitoringInitializer />
<QueryClientProvider client={queryClient}>
<Hydrate state={pageProps.dehydratedState}>
<ThemeProvider theme={defaultTheme}>
<LazyMotion features={domMax}>
<GlobalStyles />
<ErrorBoundary>
<AuthProvider>
<AuthSessionLoader>
<MonitoringInitializer />
<QueryClientProvider client={queryClient}>
<Hydrate state={pageProps.dehydratedState}>
<ThemeProvider theme={defaultTheme}>
<LazyMotion features={domMax}>
<GlobalStyles />
<ErrorBoundary>
<PageViewTracker />
<div id={MAIN_LAYOUT_ID} css={defaultLayoutCss}>
{getLayout(<Component {...pageProps} />)}
Expand All @@ -71,13 +71,13 @@ export default function App({ Component, pageProps }: AppPropsWithLayout) {
<SnackBarWrapper />
<Analytics />
</div>
</AuthProvider>
</ErrorBoundary>
</LazyMotion>
</ThemeProvider>
<ReactQueryDevtools />
</Hydrate>
</QueryClientProvider>
</ErrorBoundary>
</LazyMotion>
</ThemeProvider>
<ReactQueryDevtools />
</Hydrate>
</QueryClientProvider>
</AuthSessionLoader>
</SessionProvider>
);
}
Expand Down
19 changes: 19 additions & 0 deletions src/pages/api/auth/[...nextauth].api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,26 @@ import NextAuth from 'next-auth';
import CredentialsProvider from 'next-auth/providers/credentials';
import KakaoProvider from 'next-auth/providers/kakao';

import { post } from '~/libs/api';

interface TokenResponse {
token_type: string;
access_token: string;
}

export default NextAuth({
callbacks: {
session: async ({ session, token }) => {
const jwtTokenFromNaLabServer = await post<TokenResponse>('/v1/oauth/kakao', {
nickname: token.name,
email: token.email,
});
session.user.accessToken = jwtTokenFromNaLabServer.access_token;
session.user.tokenType = jwtTokenFromNaLabServer.token_type;

return session;
},
},
providers: [
process.env.CLOUDFLARE_ENV === 'preview'
? CredentialsProvider({
Expand Down
3 changes: 1 addition & 2 deletions src/pages/gallery/index.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useState } from 'react';
import Link from 'next/link';
import { css } from '@emotion/react';

import BottomBar from '~/components/bottomBar/BottomBar';
import Header from '~/components/header/MobileHeader';
import StaggerWrapper from '~/components/stagger/StaggerWrapper';
import Card from '~/features/gallery/Card';
Expand Down Expand Up @@ -53,7 +52,7 @@ function Gallery() {
/>
)}
</div>
<BottomBar />
{/* <BottomBar /> */}
</div>
);
}
Expand Down
3 changes: 1 addition & 2 deletions src/pages/survey/create.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useSession } from 'next-auth/react';
import { css, type Theme } from '@emotion/react';
import { useAtom, useAtomValue } from 'jotai';

import BottomBar from '~/components/bottomBar/BottomBar';
import CTAButton from '~/components/button/CTAButton';
import Header from '~/components/header/Header';
import SEO from '~/components/SEO/SEO';
Expand Down Expand Up @@ -83,7 +82,7 @@ const CreateSurveyPage = () => {

<CreateDialog isShowing={isDialogShowing} onClose={toggleDialogShowing} onAction={onSubmit} />
<CreateStopDialog isShowing={isDialogOpen} onClose={onDialogClose} onAction={onStop} />
<BottomBar />
{/* <BottomBar /> */}
</main>
</>
);
Expand Down
2 changes: 1 addition & 1 deletion src/pages/survey/join.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const JoinGuidePage = () => {
</StaggerWrapper>

<TooltipButton tooltipLabel="피드백 데이터를 간편하게 모아볼 수 있어요!">
<CTAButton css={kakaoButtonCss} onClick={loginHandler}>
<CTAButton css={kakaoButtonCss} onClick={() => loginHandler()}>
<KakaoIcon />
<span>카카오 계정으로 회원가입 하기</span>
</CTAButton>
Expand Down
1 change: 1 addition & 0 deletions src/store/auth.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { atom } from 'jotai';

// TODO: 이거 삭제
export const isUserTokenValidAtom = atom<boolean>(false);
Loading