
next-auth์useSession() ํ
:ํ์ฌ ๋ก๊ทธ์ธ๋ ์ฌ์ฉ์์ ์ธ์ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฐ ์ฌ์ฉ๋จ
ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ์์๋ง ์ฌ์ฉ ๊ฐ๋ฅํ๋ฉฐ, ์ธ์ ์ ๋ณด๋ฅผ ์ฝ๊ฒ ์ ๊ทผํ๊ณ ์ฌ์ฉํ ์ ์์
useSession() ํ
์ ์ฌ์ฉํ ๋ SessionProvider ๋ฅผ ์ ๋๋ก ์ค์ ํด์ผํจ โฉ ์ผ๋ฐ์ ์ผ๋ก app/layout.tsx ํ์ผ์์ SessionProvider ๋ฅผ ์ค์ ํ์ฌ ๋ชจ๋ ํ์ด์ง์ ์ปดํฌ๋ํธ๋ฅผ ๊ฐ์ธ๋ ๊ฒ์ด ์ผ๋ฐ์
[...nextauth]/route.ts์์ ์ธ์ฆ ์ ์ํด์ค ํ seSession()์ค์
app/components/UserGreeting.tsx
'use client';
import { signOut, useSession } from "next-auth/react";
import { useRouter } from "next/navigation";
export default function UserGreeting() {
// ์ธ์
๊ฐ์ ธ์ค๊ธฐ
const { data : session, status} = useSession();
const router = useRouter();
const goToPage = (str: string) => {
switch (str) {
case 'signup': router.push('/signup'); break;
}
};
// ์ธ์
ํ์
if (status === 'loading') {
return <span>Loading..</span>
}
if (session) {
return (
<div>
<span>{session.user?.name} ์ผ๋ก ๋ก๊ทธ์ธ</span>
<button className="btn btn-outline"
onClick={() => signOut({ callbackUrl: '/'})}>
๋ก๊ทธ์์
</button>
</div>
);
}
return (
<div>
<button className="btn btn-outline"
onClick={() => signIn()}>๋ก๊ทธ์ธ</button>
<button className="btn btn-outline"
onClick={() => goToPage('signup')}>ํ์๊ฐ์
</button>
</div>
);
}
useSession() ํ
์ ๋ ๊ฐ์ง ์ฃผ์ ๊ฐ์ ๋ฐํsession: ํ์ฌ ์ธ์
์ ๋ณด (null์ผ ์ ์์).status: ์ธ์
์ํ๋ก 'loading', 'authenticated', 'unauthenticated' ์ค ํ๋.status๊ฐ 'loading'์ด๋ฉด ๋ก๋ฉ ์ค์์ ํ์.session์ด ์์ผ๋ฉด ์ฌ์ฉ์ ์ ๋ณด์ ๋ก๊ทธ์์ ๋ฒํผ์ ํ์.session์ด ์์ผ๋ฉด ๋ก๊ทธ์ธ ๋ฒํผ์ ํ์.signIn ๋ฐ signOut ํจ์:next-auth ํจ์์์ ์ ๊ณต๋๋ ํจ์signIn(): ๋ก๊ทธ์ธ ํ์ด์ง๋ก ๋ฆฌ๋ค์ด๋ ํธํ๊ฑฐ๋ ํ์
์ ํตํด ๋ก๊ทธ์ธ. [...nextauth]/route.ts ์ pages ์ต์
์์ signIn๋ก ์ ์ํ ํ์ด์ง๋ก ์ด๋signOut(): ์ฌ์ฉ์๋ฅผ ๋ก๊ทธ์์ํ ํ signIn ํ์ด์ง๋ก ๋ฆฌ๋ค์ด๋ ํธ. ๋ค๋ฅธ ํ์ด์ง๋ก ๋ฆฌ๋ค์ด๋ ํธ ํ๋ ค๋ฉด callbackUrl ๋ก ์ง์ ํ์ฌ ํ ์ ์์useSession()์ ํ์ฉํ์ฌ ์ปค์คํ
ํ
์ ๋ง๋ค์ด ํน์ ๋ก์ง์ ๋ถ๋ฆฌํ ์ ์์ต๋๋ค.๐ญ ๋๋ ์ต์์ ๋ ์ด์์์ธ `app/layout.tsx` ์์ `SessionProvider`๋ก ๋ชจ๋ ์์ ์ปดํฌ๋ํธ๋ฅผ ๊ฐ์๋ค.
`app/layout.tsx` ์ ๋ฐ๋ก `SessionProvider` ๊ฐ์๋๋ `app/layout.tsx` ์
์ ์ํด๋ `metadata` ๋๋ฌธ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
`metadata` ๋ ์๋ฒ ์ปดํฌ๋ํธ์์๋ง ์ฌ์ฉ ํด์ผ ํ๊ณ `SessionProvider`๋ ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ์์๋ง
์ฌ์ฉํด์ผ ํ๋ค. `layout.tsx` ํ์ผ์์ `metadata`์ ํด๋ผ์ด์ธํธ ์ปดํฌ๋ํธ๋ฅผ ๋ถ๋ฆฌํ๋ค.
app/layout.tsx
import type { Metadata } from "next";
import localFont from "next/font/local";
import "./styles/globals.css";
import RootLayoutClient from "./RootLayoutClient";
const geistSans = localFont({
src: "./fonts/GeistVF.woff",
variable: "--font-geist-sans",
weight: "100 900",
});
const geistMono = localFont({
src: "./fonts/GeistMonoVF.woff",
variable: "--font-geist-mono",
weight: "100 900",
});
export const metadata: Metadata = {
title: "Chicken Map",
description: "Generated by create next app",
};
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en">
<body
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
>
<RootLayoutClient>
{children}
</RootLayoutClient>
</body>
</html>
);
}
app/RootLayoutClient.tsx
'use client';
import { SessionProvider } from "next-auth/react";
export default function RootLayoutClient({
children,
}: {
children: React.ReactNode;
}) {
return (
<SessionProvider>
{children}
</SessionProvider>
);
}
๋ก๊ทธ์ธํ๊ณ ๊ฐ๋ฐ์ ๋๊ตฌ๋ก ์ธ์
๊ฐ์ ํ์ธํ ์ ์๋ค.
Application ํญ > Storage > Cookies ์์ ํ์ธ ๊ฐ๋ฅ

signOut() ํ๋ฉด ์ธ์
ํ ํฐ ๊ฐ์ ์ฌ๋ผ์ง ๊ฒ์ ํ์ธ ํ ์ ์๋ค !