From 810b8b3b64e9611aa1275656a627fd31b28e67cd Mon Sep 17 00:00:00 2001 From: yoosangwook Date: Thu, 12 Sep 2024 15:39:55 +0900 Subject: [PATCH] Refactor session code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 기다리다 내가 먼저 푸시 한다 --- src/app/[locale]/page.js | 6 ++-- src/app/layout.js | 14 +++++--- src/components/Main.jsx | 55 +------------------------------- src/components/header/Header.jsx | 9 ++++-- src/lib/authActions.js | 8 +++++ 5 files changed, 27 insertions(+), 65 deletions(-) diff --git a/src/app/[locale]/page.js b/src/app/[locale]/page.js index bbdcd96d..b45f2690 100644 --- a/src/app/[locale]/page.js +++ b/src/app/[locale]/page.js @@ -1,21 +1,19 @@ import { getSession } from '@/lib/authActions' -import { getCurrentLocale } from '@/locales/server' +// import { getCurrentLocale } from '@/locales/server' import MainPage from '@/components/Main' export default async function page() { const session = await getSession() - const currentLocale = getCurrentLocale() + // const currentLocale = getCurrentLocale() const mainPageProps = { - currentLocale, isLoggedIn: session?.isLoggedIn, } return ( <>
-

Main

diff --git a/src/app/layout.js b/src/app/layout.js index fd3a242d..640dbe11 100644 --- a/src/app/layout.js +++ b/src/app/layout.js @@ -1,15 +1,17 @@ import { Inter } from 'next/font/google' +import { checkSession, getSession } from '@/lib/authActions' import RecoilRootWrapper from './RecoilWrapper' import UIProvider from './UIProvider' import { ToastContainer } from 'react-toastify' -import './globals.css' -import '../styles/style.scss' import Header from '@/components/header/Header' import QModal from '@/components/common/modal/QModal' +import './globals.css' +import '../styles/style.scss' + const inter = Inter({ subsets: ['latin'] }) export const metadata = { @@ -17,14 +19,18 @@ export const metadata = { description: 'Generated by create next app', } -export default function RootLayout({ children }) { +export default async function RootLayout({ children }) { + await checkSession() + const session = await getSession() + console.log('session[layout]:', session) + return ( {/*{headerPathname !== '/login' && }*/}
-
+
{children}
diff --git a/src/components/Main.jsx b/src/components/Main.jsx index 224ce1ea..4546f32d 100644 --- a/src/components/Main.jsx +++ b/src/components/Main.jsx @@ -1,62 +1,9 @@ 'use client' -import { logout } from '@/lib/authActions' -import { useChangeLocale, useI18n } from '@/locales/client' -import { Button, Chip } from '@nextui-org/react' -import Link from 'next/link' - export default function MainPage(props) { - const { currentLocale, isLoggedIn } = props - const t = useI18n() - const changeLocale = useChangeLocale() - - const handleChangeLocale = () => { - currentLocale === 'ja' ? changeLocale('ko') : changeLocale('ja') - } - - // console.log('MainPage', currentLocale) - - const handleLogout = async () => { - await logout() - } - return ( <> -

{t('locale', { locale: {currentLocale} })}

-
{t('hello')}
-
{t('welcome', { name: '효준' })}
-
- -
- {isLoggedIn && ( -
- -
- )} - {!isLoggedIn && ( -
- - - -
- )} -
font-test
- {/*

{t('locale', { locale: {currentLocale} })}

*/} - {/*
{t('hello')}
*/} - {/*
{t('welcome', { name: '효준' })}
*/} - {/*
*/} - {/* */} - {/*
*/} - {/*{isLoggedIn && (*/} - {/*
*/} - {/* */} - {/*
*/} - {/*)}*/} - {/*
font-test
*/} +

Main page

) } diff --git a/src/components/header/Header.jsx b/src/components/header/Header.jsx index f81a0ca7..5335eaab 100644 --- a/src/components/header/Header.jsx +++ b/src/components/header/Header.jsx @@ -1,8 +1,9 @@ 'use client' import Link from 'next/link' -import QSelectBox from '@/components/common/select/QSelectBox' import { usePathname } from 'next/navigation' import { useMessage } from '@/hooks/useMessage' +import QSelectBox from '@/components/common/select/QSelectBox' +import { getSession } from '@/lib/authActions' export const ToggleonMouse = (e, act, target) => { const listWrap = e.target.closest(target) @@ -19,7 +20,9 @@ export const ToggleonMouse = (e, act, target) => { } } -export default function Header() { +export default function Header(props) { + const { loginedUserNm } = props + // console.log('loginedUserNm:', loginedUserNm) const { getMessage } = useMessage() const pathName = usePathname() if (pathName.includes('login') || pathName.includes('join')) { @@ -101,7 +104,7 @@ export default function Header() {
- +
diff --git a/src/lib/authActions.js b/src/lib/authActions.js index 06c02ef0..eafddadd 100644 --- a/src/lib/authActions.js +++ b/src/lib/authActions.js @@ -27,6 +27,14 @@ export async function getSession() { return session } +export async function checkSession() { + const session = await getSession() + + if (!session.isLoggedIn) { + redirect('/login') + } +} + export async function setSession(data) { const session = await getSession()