From 586a747486f47281993c19fbf9c7991eccde25df Mon Sep 17 00:00:00 2001 From: PxlLoewe <72106766+PxlLoewe@users.noreply.github.com> Date: Sun, 16 Feb 2025 12:23:31 +0100 Subject: [PATCH] added users admin page + publicId --- apps/hub/app/(app)/admin/users/page.tsx | 31 +++++++++++++++++++ .../app/(auth)/login/_components/Login.tsx | 1 + .../(auth)/register/_components/Register.tsx | 2 +- apps/hub/app/(auth)/register/action.ts | 17 +++++++++- apps/hub/app/_components/ui/Nav.tsx | 15 ++++++++- packages/database/prisma/customs.sql | 1 + .../migrations/20250216110750_/migration.sql | 12 +++++++ packages/database/prisma/schema/user.prisma | 1 + 8 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 apps/hub/app/(app)/admin/users/page.tsx create mode 100644 packages/database/prisma/customs.sql create mode 100644 packages/database/prisma/migrations/20250216110750_/migration.sql diff --git a/apps/hub/app/(app)/admin/users/page.tsx b/apps/hub/app/(app)/admin/users/page.tsx new file mode 100644 index 00000000..14d0a87c --- /dev/null +++ b/apps/hub/app/(app)/admin/users/page.tsx @@ -0,0 +1,31 @@ +import { PaginatedTable } from '../../../_components/PaginatedTable'; + +export default async () => { + return ( + + ); +}; diff --git a/apps/hub/app/(auth)/login/_components/Login.tsx b/apps/hub/app/(auth)/login/_components/Login.tsx index 20b8660a..c4146783 100644 --- a/apps/hub/app/(auth)/login/_components/Login.tsx +++ b/apps/hub/app/(auth)/login/_components/Login.tsx @@ -28,6 +28,7 @@ export const Login = () => { onSubmit={form.handleSubmit(async () => { setIsLoading(true); const data = await signIn('credentials', { + redirect: false, callbackUrl: searchParams.get('redirect') || '/', email: form.getValues('email'), password: form.getValues('password'), diff --git a/apps/hub/app/(auth)/register/_components/Register.tsx b/apps/hub/app/(auth)/register/_components/Register.tsx index cb4871d1..c38d36e5 100644 --- a/apps/hub/app/(auth)/register/_components/Register.tsx +++ b/apps/hub/app/(auth)/register/_components/Register.tsx @@ -56,7 +56,7 @@ export const Register = () => { lastname: form.getValues('lastname'), }); await signIn('credentials', { - redirect: false, + callbackUrl: '/', email: user.email, password: values.password, }); diff --git a/apps/hub/app/(auth)/register/action.ts b/apps/hub/app/(auth)/register/action.ts index 805831fb..1afeb7ac 100644 --- a/apps/hub/app/(auth)/register/action.ts +++ b/apps/hub/app/(auth)/register/action.ts @@ -5,11 +5,26 @@ import bcrypt from 'bcryptjs'; export const register = async ({ password, ...user -}: Prisma.UserCreateInput) => { +}: Omit) => { const hashedPassword = await bcrypt.hash(password, 15); + const lastUserPublicId = await prisma.user.findFirst({ + select: { + publicId: true, + }, + orderBy: { + publicId: 'desc', + }, + }); + let varPublicId = 'VAR0000'; + if (lastUserPublicId) { + const lastUserInt = parseInt(lastUserPublicId.publicId.replace('VAR', '')); + varPublicId = `VAR${(lastUserInt + 1).toString().padStart(4, '0')}`; + } + const newUser = prisma.user.create({ data: { ...user, + publicId: varPublicId, password: hashedPassword, }, }); diff --git a/apps/hub/app/_components/ui/Nav.tsx b/apps/hub/app/_components/ui/Nav.tsx index 8bac586d..3c74ba35 100644 --- a/apps/hub/app/_components/ui/Nav.tsx +++ b/apps/hub/app/_components/ui/Nav.tsx @@ -3,6 +3,7 @@ import { PersonIcon, GearIcon, ExitIcon, + LockClosedIcon, } from '@radix-ui/react-icons'; import Link from 'next/link'; @@ -20,7 +21,19 @@ export const VerticalNav = () => { Profile -
  • +
  • +
    + + + Admin + +
      +
    • + User +
    • +
    +
    +
  • diff --git a/packages/database/prisma/customs.sql b/packages/database/prisma/customs.sql new file mode 100644 index 00000000..20314e5d --- /dev/null +++ b/packages/database/prisma/customs.sql @@ -0,0 +1 @@ +CREATE SEQUENCE user_custom_id_seq START 1; \ No newline at end of file diff --git a/packages/database/prisma/migrations/20250216110750_/migration.sql b/packages/database/prisma/migrations/20250216110750_/migration.sql new file mode 100644 index 00000000..2abf83a9 --- /dev/null +++ b/packages/database/prisma/migrations/20250216110750_/migration.sql @@ -0,0 +1,12 @@ +/* + Warnings: + + - A unique constraint covering the columns `[publicId]` on the table `users` will be added. If there are existing duplicate values, this will fail. + - Added the required column `publicId` to the `users` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE "users" ADD COLUMN "publicId" TEXT NOT NULL; + +-- CreateIndex +CREATE UNIQUE INDEX "users_publicId_key" ON "users"("publicId"); diff --git a/packages/database/prisma/schema/user.prisma b/packages/database/prisma/schema/user.prisma index 84467cdd..bfd4258c 100644 --- a/packages/database/prisma/schema/user.prisma +++ b/packages/database/prisma/schema/user.prisma @@ -1,5 +1,6 @@ model User { id String @id @default(uuid()) + publicId String @unique firstname String lastname String email String @unique