Refactor session code
기다리다 내가 먼저 푸시 한다
This commit is contained in:
parent
c024a893cd
commit
810b8b3b64
@ -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 (
|
||||
<>
|
||||
<div className="m-4">
|
||||
<h3>Main</h3>
|
||||
<MainPage {...mainPageProps} />
|
||||
</div>
|
||||
</>
|
||||
|
||||
@ -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 (
|
||||
<html lang="en">
|
||||
<body className={inter.className}>
|
||||
{/*{headerPathname !== '/login' && <Headers />}*/}
|
||||
<RecoilRootWrapper>
|
||||
<div className="wrap">
|
||||
<Header />
|
||||
<Header loginedUserNm={session.userNm} />
|
||||
<div className="content">
|
||||
<UIProvider>{children}</UIProvider>
|
||||
</div>
|
||||
|
||||
@ -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 (
|
||||
<>
|
||||
<h3>{t('locale', { locale: <Chip color="primary">{currentLocale}</Chip> })}</h3>
|
||||
<div>{t('hello')}</div>
|
||||
<div>{t('welcome', { name: '효준' })}</div>
|
||||
<div>
|
||||
<Button onClick={handleChangeLocale}>Change Locale</Button>
|
||||
</div>
|
||||
{isLoggedIn && (
|
||||
<div className="my-4">
|
||||
<Button color="primary" onClick={handleLogout}>
|
||||
로그아웃
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
{!isLoggedIn && (
|
||||
<div className="my-4">
|
||||
<Link href={'/' + currentLocale + '/login'}>
|
||||
<Button color="primary">로그인 페이지로 이동</Button>
|
||||
</Link>
|
||||
</div>
|
||||
)}
|
||||
<div className="font-test">font-test</div>
|
||||
{/*<h3>{t('locale', { locale: <Chip color="primary">{currentLocale}</Chip> })}</h3>*/}
|
||||
{/*<div>{t('hello')}</div>*/}
|
||||
{/*<div>{t('welcome', { name: '효준' })}</div>*/}
|
||||
{/*<div>*/}
|
||||
{/* <Button onClick={handleChangeLocale}>Change Locale</Button>*/}
|
||||
{/*</div>*/}
|
||||
{/*{isLoggedIn && (*/}
|
||||
{/* <div className="my-4">*/}
|
||||
{/* <Button color="primary" onClick={handleLogout}>*/}
|
||||
{/* 로그아웃*/}
|
||||
{/* </Button>*/}
|
||||
{/* </div>*/}
|
||||
{/*)}*/}
|
||||
{/*<div className="font-test">font-test</div>*/}
|
||||
<h1>Main page</h1>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
@ -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() {
|
||||
</div>
|
||||
<div className="header-left">
|
||||
<div className="profile-box">
|
||||
<button className="profile">Kim Ji Young</button>
|
||||
<button className="profile">{loginedUserNm}</button>
|
||||
</div>
|
||||
<div className="sign-out-box">
|
||||
<button className="sign-out">{getMessage('header.logout')}</button>
|
||||
|
||||
@ -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()
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user