From a15481b03d121fdf54258e483145466579643182 Mon Sep 17 00:00:00 2001 From: Keith Stevens Date: Wed, 18 Jan 2023 14:41:29 +0900 Subject: [PATCH 1/4] Cleaning up the username api route and ensuring the users name is fresh --- website/src/pages/api/auth/[...nextauth].ts | 5 +++-- website/src/pages/api/username.tsx | 22 ++++++++++----------- 2 files changed, 14 insertions(+), 13 deletions(-) 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; From 5fa1155deb98ee241fffcf1f209eb6851e33dad5 Mon Sep 17 00:00:00 2001 From: Keith Stevens Date: Wed, 18 Jan 2023 14:51:06 +0900 Subject: [PATCH 2/4] Deleting a decayed cypress auth check --- website/cypress/e2e/auth/signin.cy.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/website/cypress/e2e/auth/signin.cy.ts b/website/cypress/e2e/auth/signin.cy.ts index 6d57d1f9..2a651f1f 100644 --- a/website/cypress/e2e/auth/signin.cy.ts +++ b/website/cypress/e2e/auth/signin.cy.ts @@ -27,13 +27,6 @@ describe("signin flow", () => { }); }); }); - it("shows the logged in users email address if logged in with email", () => { - const emailAddress = "user@example.com"; - cy.signInWithEmail(emailAddress); - // The user will only see the email address if the window is wide enough, not technically required as even when hidden this will find it in the page. - cy.viewport(1920, 1000); - cy.contains('[data-cy="username"]', emailAddress); - }); }); export {}; From 173af6847ec928134b666cfd757b38e46e16b364 Mon Sep 17 00:00:00 2001 From: Keith Stevens Date: Wed, 18 Jan 2023 15:23:24 +0900 Subject: [PATCH 3/4] Moving the username handler to a ts file --- website/src/pages/api/{username.tsx => username.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename website/src/pages/api/{username.tsx => username.ts} (100%) diff --git a/website/src/pages/api/username.tsx b/website/src/pages/api/username.ts similarity index 100% rename from website/src/pages/api/username.tsx rename to website/src/pages/api/username.ts From a5d59339f9c4a17635f86d85d88ee2aaa85e01f3 Mon Sep 17 00:00:00 2001 From: Keith Stevens Date: Wed, 18 Jan 2023 15:33:27 +0900 Subject: [PATCH 4/4] Deleting some function arguments used for debugging --- website/src/pages/api/auth/[...nextauth].ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/src/pages/api/auth/[...nextauth].ts b/website/src/pages/api/auth/[...nextauth].ts index 6c6ac21c..3d3dbaa4 100644 --- a/website/src/pages/api/auth/[...nextauth].ts +++ b/website/src/pages/api/auth/[...nextauth].ts @@ -88,7 +88,7 @@ export const authOptions: AuthOptions = { * Ensure we propagate the user's role when creating the session from the * token. */ - async session({ session, user, token }) { + async session({ session, token }) { session.user.role = token.role; session.user.isNew = token.isNew; session.user.name = token.name; @@ -98,7 +98,7 @@ export const authOptions: AuthOptions = { * 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, user, account }) { + async jwt({ token }) { const { isNew, name, role } = await prisma.user.findUnique({ where: { id: token.sub }, select: { name: true, role: true, isNew: true },