|
| 1 | +import { prisma } from "@/prisma"; |
| 2 | +import { notFound, redirect } from 'next/navigation'; |
| 3 | +import { NavigationMenu } from "../components/navigationMenu"; |
| 4 | +import { auth } from "@/auth"; |
| 5 | +import { getUser } from "@/data/user"; |
| 6 | +import { AcceptInviteButton } from "./components/acceptInviteButton" |
| 7 | + |
| 8 | +interface RedeemPageProps { |
| 9 | + searchParams?: { |
| 10 | + invite_id?: string; |
| 11 | + }; |
| 12 | + } |
| 13 | + |
| 14 | + export default async function RedeemPage({ searchParams }: RedeemPageProps) { |
| 15 | + const invite_id = searchParams?.invite_id; |
| 16 | + |
| 17 | + if (!invite_id) { |
| 18 | + notFound(); |
| 19 | + } |
| 20 | + |
| 21 | + const invite = await prisma.invite.findUnique({ |
| 22 | + where: { id: invite_id }, |
| 23 | + }); |
| 24 | + |
| 25 | + if (!invite) { |
| 26 | + return ( |
| 27 | + <div> |
| 28 | + <NavigationMenu /> |
| 29 | + <div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '100vh' }}> |
| 30 | + <h1>This invite either expired or was revoked. Contact your organization owner.</h1> |
| 31 | + </div> |
| 32 | + </div> |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + const session = await auth(); |
| 37 | + let user = undefined; |
| 38 | + if (session) { |
| 39 | + user = await getUser(session.user.id); |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + // Auth case |
| 44 | + if (user) { |
| 45 | + if (user.email !== invite.recipientEmail) { |
| 46 | + return ( |
| 47 | + <div> |
| 48 | + <NavigationMenu /> |
| 49 | + <div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '100vh' }}> |
| 50 | + <h1>Sorry this invite does not belong to you.</h1> |
| 51 | + </div> |
| 52 | + </div> |
| 53 | + ) |
| 54 | + } else { |
| 55 | + const orgName = await prisma.org.findUnique({ |
| 56 | + where: { id: invite.orgId }, |
| 57 | + select: { name: true }, |
| 58 | + }); |
| 59 | + |
| 60 | + if (!orgName) { |
| 61 | + return ( |
| 62 | + <div> |
| 63 | + <NavigationMenu /> |
| 64 | + <div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '100vh' }}> |
| 65 | + <h1>Organization not found. Please contact the invite sender.</h1> |
| 66 | + </div> |
| 67 | + </div> |
| 68 | + ) |
| 69 | + } |
| 70 | + |
| 71 | + return ( |
| 72 | + <div> |
| 73 | + <NavigationMenu /> |
| 74 | + <div className="flex justify-between items-center h-screen px-6"> |
| 75 | + <h1 className="text-2xl font-bold">You've been invited to org {orgName.name}</h1> |
| 76 | + <AcceptInviteButton invite={invite} userId={user.id} /> |
| 77 | + </div> |
| 78 | + </div> |
| 79 | + ); |
| 80 | + } |
| 81 | + } else { |
| 82 | + redirect(`/login?callbackUrl=${encodeURIComponent(`/redeem?invite_id=${invite_id}`)}`); |
| 83 | + } |
| 84 | +} |
0 commit comments