diff --git a/website/src/pages/api/auth/[...nextauth].ts b/website/src/pages/api/auth/[...nextauth].ts index c718ddce..6c6ac21c 100644 --- a/website/src/pages/api/auth/[...nextauth].ts +++ b/website/src/pages/api/auth/[...nextauth].ts @@ -88,16 +88,17 @@ export const authOptions: AuthOptions = { * Ensure we propagate the user's role when creating the session from the * token. */ - async session({ session, token }) { + async session({ session, user, token }) { session.user.role = token.role; session.user.isNew = token.isNew; + session.user.name = token.name; return session; }, /** * When creating a token, fetch the user's role and inject it in the token. * This let's use forward the role to the session object. */ - async jwt({ token }) { + async jwt({ token, user, account }) { const { isNew, name, role } = await prisma.user.findUnique({ where: { id: token.sub }, select: { name: true, role: true, isNew: true }, diff --git a/website/src/pages/api/username.tsx b/website/src/pages/api/username.tsx index 6cf362c2..ba71c4b5 100644 --- a/website/src/pages/api/username.tsx +++ b/website/src/pages/api/username.tsx @@ -1,20 +1,20 @@ -import { getSession } from "next-auth/react"; +import { withoutRole } from "src/lib/auth"; import prisma from "src/lib/prismadb"; -// POST /api/post -// Required fields in body: title -// Optional fields in body: content -export default async function handle(req, res) { +/** + * Updates the user's `name` field in the `User` table. + */ +const handler = withoutRole("banned", async (req, res, token) => { const { username } = req.body; - - const session = await getSession({ req }); - const result = await prisma.user.update({ + const { name } = await prisma.user.update({ where: { - email: session.user.email, + id: token.sub, }, data: { name: username, }, }); - res.json({ name: result.name }); -} + res.json({ name }); +}); + +export default handler;