mirror of
https://github.com/SrIzan10/next-auth.git
synced 2026-05-01 10:55:20 +00:00
36 lines
833 B
JavaScript
36 lines
833 B
JavaScript
import { useState, useEffect } from "react"
|
|
import { useSession } from "next-auth/react"
|
|
import Layout from "../components/layout"
|
|
|
|
export default function Page() {
|
|
const { status } = useSession({
|
|
required: true,
|
|
})
|
|
const [content, setContent] = useState()
|
|
|
|
// Fetch content from protected route
|
|
useEffect(() => {
|
|
if (status === "loading") return
|
|
const fetchData = async () => {
|
|
const res = await fetch("/api/examples/protected")
|
|
const json = await res.json()
|
|
if (json.content) {
|
|
setContent(json.content)
|
|
}
|
|
}
|
|
fetchData()
|
|
}, [status])
|
|
|
|
if (status === "loading") return <Layout>Loading...</Layout>
|
|
|
|
// If session exists, display content
|
|
return (
|
|
<Layout>
|
|
<h1>Protected Page</h1>
|
|
<p>
|
|
<strong>{content}</strong>
|
|
</p>
|
|
</Layout>
|
|
)
|
|
}
|