From 7c1e26b1502bb62dec0eb2f4e69b596a87fbbcc1 Mon Sep 17 00:00:00 2001 From: greysoh Date: Sun, 5 May 2024 21:56:35 -0400 Subject: [PATCH] feature: Adds username support. LOM eta wen --- .../migration.sql | 2 ++ api/prisma/schema.prisma | 3 ++- api/src/routes/user/create.ts | 6 +++++- api/src/routes/user/login.ts | 13 ++++++++++--- api/src/routes/user/lookup.ts | 4 ++++ 5 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 api/prisma/migrations/20240505233740_feature_adds_username_support/migration.sql diff --git a/api/prisma/migrations/20240505233740_feature_adds_username_support/migration.sql b/api/prisma/migrations/20240505233740_feature_adds_username_support/migration.sql new file mode 100644 index 0000000..5af7c52 --- /dev/null +++ b/api/prisma/migrations/20240505233740_feature_adds_username_support/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "User" ADD COLUMN "username" TEXT; diff --git a/api/prisma/schema.prisma b/api/prisma/schema.prisma index 343af83..486f3e3 100644 --- a/api/prisma/schema.prisma +++ b/api/prisma/schema.prisma @@ -45,9 +45,10 @@ model User { id Int @id @default(autoincrement()) email String @unique + username String? // NOT optional in the API, but just for backwards compat name String password String // Will be hashed using bcrypt rootToken String? isRootServiceAccount Boolean? permissions Permission[] -} +} \ No newline at end of file diff --git a/api/src/routes/user/create.ts b/api/src/routes/user/create.ts index 4259421..817f917 100644 --- a/api/src/routes/user/create.ts +++ b/api/src/routes/user/create.ts @@ -17,10 +17,11 @@ export function route(routeOptions: RouteOptions) { schema: { body: { type: "object", - required: ["name", "email", "password"], + required: ["name", "email", "username", "password"], properties: { name: { type: "string" }, + username: { type: "string" }, email: { type: "string" }, password: { type: "string" }, }, @@ -33,6 +34,7 @@ export function route(routeOptions: RouteOptions) { name: string; email: string; password: string; + username: string; } = req.body; if (!options.isSignupEnabled) { @@ -60,6 +62,8 @@ export function route(routeOptions: RouteOptions) { email: body.email, password: saltedPassword, + username: body.username, + permissions: { create: [] as { permission: string; diff --git a/api/src/routes/user/login.ts b/api/src/routes/user/login.ts index 14b04c3..68c5a1e 100644 --- a/api/src/routes/user/login.ts +++ b/api/src/routes/user/login.ts @@ -15,11 +15,12 @@ export function route(routeOptions: RouteOptions) { schema: { body: { type: "object", - required: ["email", "password"], + required: ["password"], properties: { email: { type: "string" }, - password: { type: "string" }, + username: { type: "string" }, + password: { type: "string" } }, }, }, @@ -27,13 +28,19 @@ export function route(routeOptions: RouteOptions) { async (req, res) => { // @ts-ignore const body: { - email: string; + email?: string; + username?: string; password: string; } = req.body; + if (!body.email && !body.username) return res.status(400).send({ + error: "missing both email and username. please supply at least one." + }); + const userSearch = await prisma.user.findFirst({ where: { email: body.email, + username: body.username }, }); diff --git a/api/src/routes/user/lookup.ts b/api/src/routes/user/lookup.ts index e9b00fe..02e3093 100644 --- a/api/src/routes/user/lookup.ts +++ b/api/src/routes/user/lookup.ts @@ -24,6 +24,7 @@ export function route(routeOptions: RouteOptions) { id: { type: "number" }, name: { type: "string" }, email: { type: "string" }, + username: { type: "string" }, isServiceAccount: { type: "boolean" }, }, }, @@ -36,6 +37,7 @@ export function route(routeOptions: RouteOptions) { id?: number; name?: string; email?: string; + username?: string; isServiceAccount?: boolean; } = req.body; @@ -50,6 +52,7 @@ export function route(routeOptions: RouteOptions) { id: body.id, name: body.name, email: body.email, + username: body.username, isRootServiceAccount: body.isServiceAccount, }, }); @@ -60,6 +63,7 @@ export function route(routeOptions: RouteOptions) { name: i.name, email: i.email, isServiceAccount: i.isRootServiceAccount, + username: i.username })), }; },