mirror of
https://github.com/SrIzan10/next-auth.git
synced 2026-05-01 10:55:20 +00:00
* chore: dev -> dev/nextjs * chore: move to /examples * chore: move to playgrounds, add dev/sveltekit * Update sync.yml * chore: dev scripts
41 lines
946 B
TypeScript
41 lines
946 B
TypeScript
import { useState, useEffect } from "react"
|
|
import { useSession } from "next-auth/react"
|
|
import Layout from "../components/layout"
|
|
import AccessDenied from "../components/access-denied"
|
|
|
|
export default function ProtectedPage() {
|
|
const { data: session } = useSession()
|
|
const [content, setContent] = useState()
|
|
|
|
// Fetch content from protected route
|
|
useEffect(() => {
|
|
const fetchData = async () => {
|
|
const res = await fetch("/api/examples/protected")
|
|
const json = await res.json()
|
|
if (json.content) {
|
|
setContent(json.content)
|
|
}
|
|
}
|
|
fetchData()
|
|
}, [session])
|
|
|
|
// If no session exists, display access denied message
|
|
if (!session) {
|
|
return (
|
|
<Layout>
|
|
<AccessDenied />
|
|
</Layout>
|
|
)
|
|
}
|
|
|
|
// If session exists, display content
|
|
return (
|
|
<Layout>
|
|
<h1>Protected Page</h1>
|
|
<p>
|
|
<strong>{content ?? "\u00a0"}</strong>
|
|
</p>
|
|
</Layout>
|
|
)
|
|
}
|