Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

Added logic to automatically parse url and validate token, also check… (#950)

Amanuel Odicho5d6fb4e1fd91
diff --git a/apps/web/src/app/[locale]/claim/_actions.ts b/apps/web/src/app/[locale]/claim/_actions.ts
index 765500a3..1872d1a9 100644
--- a/apps/web/src/app/[locale]/claim/_actions.ts
+++ b/apps/web/src/app/[locale]/claim/_actions.ts
@@ -1,6 +1,5 @@
'use server';
-
-import { getServerAuthSession } from '@repo/auth/server';
+import { getServerAuthSession, type Session } from '@repo/auth/server';
import { prisma } from '@repo/db';
import { redirect } from 'next/navigation';
import { claimFormSchema } from './_schema';
@@ -29,3 +28,24 @@ export async function validateToken(claimFormData: FormSchema) {
redirect('/explore');
}
+
+export async function isValidToken(session: Session, claimFormData: FormSchema) {
+ try {
+ claimFormSchema.parse(claimFormData);
+
+ await prisma.betaTokens.update({
+ where: {
+ token: claimFormData.code,
+ AND: {
+ userId: null,
+ },
+ },
+ data: {
+ user: { connect: { id: session?.user.id } },
+ },
+ });
+ return true;
+ } catch (e) {
+ return false;
+ }
+}
diff --git a/apps/web/src/app/[locale]/claim/page.tsx b/apps/web/src/app/[locale]/claim/page.tsx
index 8638a517..2cdcee0c 100644
--- a/apps/web/src/app/[locale]/claim/page.tsx
+++ b/apps/web/src/app/[locale]/claim/page.tsx
@@ -1,10 +1,32 @@
-import { getServerAuthSession } from '@repo/auth/server';
+import { getServerAuthSession, type Session } from '@repo/auth/server';
import { LoginButton } from '../login/_components/LoginButton';
-import { ClaimForm } from './claim-form';
+import { ClaimForm, type FormSchema } from './claim-form';
import Image from 'next/image';
+import { isBetaUser } from '~/utils/server/is-beta-user';
+import { redirect } from 'next/navigation';
+import { isValidToken } from './_actions';
-export default async function Claim() {
+interface Props {
+ searchParams?: { token: string };
+}
+
+export default async function Claim({ searchParams }: Props) {
+ const token = searchParams?.token;
const session = await getServerAuthSession();
+
+ if (session && token) {
+ if (await isValidToken(session, { code: token })) {
+ return redirect('/explore');
+ }
+ }
+
+ const isBeta = await isBetaUser(session);
+ if (isBeta) {
+ return redirect('/explore');
+ }
+
+ const redirectUrl = token ? `/claim?token=${token}` : '/claim';
+
const header = session ? 'In early access?' : 'Login to claim your token';
const subheader = session ? 'Enter the token that was emailed to you' : '';
return (
@@ -22,7 +44,7 @@ export default async function Claim() {
{header}
</h1>
<p>{subheader}</p>
- {session ? <ClaimForm /> : <LoginButton redirectTo="/claim" />}
+ {session ? <ClaimForm /> : <LoginButton redirectTo={redirectUrl} />}
</div>
</div>
</div>