From b02429c1f7f9699e10c036fb002abc5b14ce6cfc Mon Sep 17 00:00:00 2001 From: Keith Stevens Date: Sat, 14 Jan 2023 18:55:28 +0900 Subject: [PATCH] Tag in the user table when they are new and include it in the user session --- website/prisma/schema.prisma | 1 + website/src/pages/api/auth/[...nextauth].ts | 6 ++++-- website/src/pages/api/update_task.ts | 3 +++ website/src/pages/dashboard.tsx | 6 ++++++ website/types/next-auth.d.ts | 4 ++++ 5 files changed, 18 insertions(+), 2 deletions(-) diff --git a/website/prisma/schema.prisma b/website/prisma/schema.prisma index f9eab3b7..dc6a700d 100644 --- a/website/prisma/schema.prisma +++ b/website/prisma/schema.prisma @@ -41,6 +41,7 @@ model User { email String? @unique emailVerified DateTime? image String? + isNew Boolean @default(true) role String @default("general") accounts Account[] diff --git a/website/src/pages/api/auth/[...nextauth].ts b/website/src/pages/api/auth/[...nextauth].ts index 363c1404..254e4f9d 100644 --- a/website/src/pages/api/auth/[...nextauth].ts +++ b/website/src/pages/api/auth/[...nextauth].ts @@ -86,6 +86,7 @@ export const authOptions: AuthOptions = { */ async session({ session, token }) { session.user.role = token.role; + session.user.isNew = token.isNew; return session; }, /** @@ -93,11 +94,12 @@ export const authOptions: AuthOptions = { * This let's use forward the role to the session object. */ async jwt({ token }) { - const { role } = await prisma.user.findUnique({ + const { isNew, role } = await prisma.user.findUnique({ where: { id: token.sub }, - select: { role: true }, + select: { role: true, isNew: true }, }); token.role = role; + token.isNew = isNew; return token; }, }, diff --git a/website/src/pages/api/update_task.ts b/website/src/pages/api/update_task.ts index f3aa6fb1..02982daa 100644 --- a/website/src/pages/api/update_task.ts +++ b/website/src/pages/api/update_task.ts @@ -17,6 +17,9 @@ const handler = withoutRole("banned", async (req, res, token) => { // Parse out the local task ID and the interaction contents. const { id: frontendId, content, update_type } = req.body; + // Record that the user has done meaningful work and is no longer new. + await prisma.user.update({ where: { id: token.sub }, data: { isNew: false } }); + // Accept the task so that we can complete it, this will probably go away soon. const registeredTask = await prisma.registeredTask.findUniqueOrThrow({ where: { id: frontendId } }); const task = registeredTask.task as Prisma.JsonObject; diff --git a/website/src/pages/dashboard.tsx b/website/src/pages/dashboard.tsx index d296592d..42481f1e 100644 --- a/website/src/pages/dashboard.tsx +++ b/website/src/pages/dashboard.tsx @@ -1,9 +1,15 @@ import Head from "next/head"; +import { useSession } from "next-auth/react"; import { LeaderboardTable, TaskOption } from "src/components/Dashboard"; import { getDashboardLayout } from "src/components/Layout"; import { TaskCategory } from "src/components/Tasks/TaskTypes"; const Dashboard = () => { + const { data: session } = useSession(); + + // TODO(#670): Do something more meaningful when the user is new. + console.log(session?.user?.isNew); + return ( <> diff --git a/website/types/next-auth.d.ts b/website/types/next-auth.d.ts index 25027600..738ff8f2 100644 --- a/website/types/next-auth.d.ts +++ b/website/types/next-auth.d.ts @@ -6,6 +6,8 @@ declare module "next-auth" { user: { /** The user's role. */ role: string; + /** True when the user is new. */ + isNew: boolean; } & DefaultSession["user"]; } } @@ -14,5 +16,7 @@ declare module "next-auth/jwt" { interface JWT { /** The user's role. */ role?: string; + /** True when the user is new. */ + isNew?: boolean; } }