forked from thirdweb-dev/js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgated-content.tsx
95 lines (86 loc) · 2.87 KB
/
gated-content.tsx
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { getAuthResult } from "@/app/connect/auth/server/actions/auth";
import { THIRDWEB_CLIENT } from "@/lib/client";
import { cookies } from "next/headers";
import Link from "next/link";
import { getContract } from "thirdweb";
import { sepolia } from "thirdweb/chains";
import { balanceOf } from "thirdweb/extensions/erc20";
import { AuthButton } from "./auth-button";
export async function GatedContentPreview() {
const jwt = (await cookies()).get("jwt");
const authResult = jwt?.value ? await getAuthResult(jwt.value) : undefined;
if (!authResult) {
return (
<div className="flex flex-col gap-5">
<div className="mx-auto">
<AuthButton />
</div>
<div className="text-center">Log in to see the secret content</div>
</div>
);
}
// If the user has logged in, get their wallet address
const address = authResult.parsedJWT.sub;
if (!address) throw new Error("could not get wallet address");
// This is the part that we do the gating condition.
// If pass -> Allow them to access the page.
async function hasEnoughBalance(userAddress: string) {
const erc20Contract = getContract({
address: "0xACf072b740a23D48ECd302C9052fbeb3813b60a6",
chain: sepolia,
client: THIRDWEB_CLIENT,
});
const requiredQuantity = 10n; // 10 erc20 token
const ownedBalance = await balanceOf({
contract: erc20Contract,
address: userAddress,
});
return ownedBalance < requiredQuantity;
}
// For this example, we check if a user has more than 10 $TWCOIN
if (await hasEnoughBalance(authResult.parsedJWT.sub)) {
return (
<div className="flex flex-col gap-5">
<div className="mx-auto">
<AuthButton />
</div>
<div className="mx-auto px-3 text-center lg:max-w-[450px]">
You are logged in. However you cannot see the secret content because
you own less than 10 $TWCOIN.
<br />
Mint some tokens{" "}
<Link
href="/connect/blockchain-api"
className="font-bold text-yellow-400"
>
here
</Link>
</div>
</div>
);
}
// Finally! We can load the gated content for them now
return (
<div className="flex flex-col gap-5">
<div className="mx-auto">
<AuthButton />
</div>
<div className="text-center font-bold text-green-600 text-lg">
Congratulations!
</div>
<div className="mx-auto px-3 text-center">
You can see this message because you own more than 10 TWCOIN.
<br />
Mint a free commemorative NFT{" "}
<a
href="https://thirdweb.com/arbitrum/0xE7d6D628163de95D1c72c343ee852539B51f35Dc/nfts/0"
rel="noreferrer"
target="_blank"
className="font-bold text-yellow-400 underline"
>
here
</a>
</div>
</div>
);
}