Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { useState, useEffect } from "react"
import { useSession } from "next-auth/react"
import styles from '@/styles/Home.module.css'
import Link from "next/link"
import { formatBalance } from '@polkadot/util';
import PolkadotParticles from "@/components/polkadot-particles"
/**
* This is a protected page, it can only be accessed by authenticated users. Instead of using Server Side
* Rendering (SSR) to fetch the content, we use Static Generation and a protected API Route `/pages/api/auth.ts`
* to fetch the content client side after the user has logged in.
*/
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/auth")
if (res.ok) {
const json = await res.json()
if (json?.content) {
setContent(json.content)
}
}
}
fetchData()
}, [session])
// If no session exists, display access denied message
if (!session) {
return (
<main className={ styles.protected }>
<h1>You did not pass the 🚪</h1>
<p><Link href="/" className={ styles.colorA }>< go back</Link></p>
</main>
)
}
// format the big number to a human readable format
const ksmBalance = formatBalance( session.freeBalance, { decimals: 12, withSi: true, withUnit: 'KSM' } )
// If session exists, display content
return (
<main className={ styles.protected }>
<h1>🎉 Welcome { session.user?.name }, you passed the 🚪</h1>
<p>with { ksmBalance }</p>
<p>You are seeing a protected route that uses a static page and a protected api route. See the code at <code>/pages/protected.tsx</code></p>
<p> <Link href="/" className={ styles.colorA }>< go back</Link></p>
<PolkadotParticles />
</main>
)
}