diff --git a/bun.lockb b/bun.lockb index 4d06590..509b39c 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 0f47b3c..b9dbb60 100644 --- a/package.json +++ b/package.json @@ -3,10 +3,10 @@ "version": "0.0.1", "type": "module", "scripts": { - "dev": "bun validate-env && bun database:up && bun prisma:generate && bun prisma:push && vite dev", + "dev": "bun validate-env && bun database:up && bun prisma:generate && vite dev", "build": "vite build", "build-storybook": "storybook build", - "database:up": "docker compose -p hestia -f devops/docker-compose.dev.yml up -d && docker compose -p hestia -f devops/docker-compose.dev.yml -f devops/docker-compose.wait.yml run --rm wait -c hestia-database:5432", + "database:up": "docker compose -p hestia -f devops/docker-compose.dev.yml up -d && docker compose -p hestia -f devops/docker-compose.dev.yml -f devops/docker-compose.wait.yml run --rm wait -c hestia-database:5432 && bun prisma:push", "database:down": "docker compose -p hestia -f devops/docker-compose.dev.yml down", "preview": "vite preview", "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json", @@ -65,8 +65,7 @@ "vitest": "^2.0.4" }, "dependencies": { - "@clerk/clerk-js": "^5.43.4", - "@clerk/express": "^1.3.31", + "@clerk/backend": "1.21.4", "@clerk/themes": "^2.2.3", "@flaticon/flaticon-uicons": "^3.3.1", "@inlang/paraglide-sveltekit": "^0.15.0", diff --git a/prisma/migrations/20250101171120_update_user_table/migration.sql b/prisma/migrations/20250101171120_update_user_table/migration.sql deleted file mode 100644 index 1f6c706..0000000 --- a/prisma/migrations/20250101171120_update_user_table/migration.sql +++ /dev/null @@ -1,18 +0,0 @@ -/* - Warnings: - - - You are about to drop the column `password` on the `User` table. All the data in the column will be lost. - - You are about to drop the `Session` table. If the table is not empty, all the data it contains will be lost. - -*/ --- DropForeignKey -ALTER TABLE "Session" DROP CONSTRAINT "Session_userId_fkey"; - --- DropIndex -DROP INDEX "User_email_key"; - --- AlterTable -ALTER TABLE "User" DROP COLUMN "password"; - --- DropTable -DROP TABLE "Session"; diff --git a/prisma/migrations/20250101171527_add_clerk_id_to_user_table/migration.sql b/prisma/migrations/20250101171527_add_clerk_id_to_user_table/migration.sql deleted file mode 100644 index 2c52947..0000000 --- a/prisma/migrations/20250101171527_add_clerk_id_to_user_table/migration.sql +++ /dev/null @@ -1,8 +0,0 @@ -/* - Warnings: - - - Added the required column `clerkId` to the `User` table without a default value. This is not possible if the table is not empty. - -*/ --- AlterTable -ALTER TABLE "User" ADD COLUMN "clerkId" TEXT NOT NULL; diff --git a/prisma/migrations/20250105021234_add_clerk_auth/migration.sql b/prisma/migrations/20250105021234_add_clerk_auth/migration.sql new file mode 100644 index 0000000..a37b472 --- /dev/null +++ b/prisma/migrations/20250105021234_add_clerk_auth/migration.sql @@ -0,0 +1,24 @@ +/* + Warnings: + + - You are about to drop the column `password` on the `User` table. All the data in the column will be lost. + - You are about to drop the `Session` table. If the table is not empty, all the data it contains will be lost. + - A unique constraint covering the columns `[clerkId]` on the table `User` will be added. If there are existing duplicate values, this will fail. + - Added the required column `clerkId` to the `User` table without a default value. This is not possible if the table is not empty. + +*/ +-- DropForeignKey +ALTER TABLE "Session" DROP CONSTRAINT "Session_userId_fkey"; + +-- DropIndex +DROP INDEX "User_email_key"; + +-- AlterTable +ALTER TABLE "User" DROP COLUMN "password", +ADD COLUMN "clerkId" TEXT NOT NULL; + +-- DropTable +DROP TABLE "Session"; + +-- CreateIndex +CREATE UNIQUE INDEX "User_clerkId_key" ON "User"("clerkId"); diff --git a/prisma/schema.prisma b/prisma/schema.prisma index d23b90b..090ea2e 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -16,7 +16,7 @@ datasource db { model User { id String @id @default(uuid()) - clerkId String + clerkId String @unique email String? name String diff --git a/src/lib/server/auth/index.ts b/src/lib/server/auth/index.ts index a2ab010..8bc10b9 100644 --- a/src/lib/server/auth/index.ts +++ b/src/lib/server/auth/index.ts @@ -1,15 +1,16 @@ import { redirect, type ServerLoadEvent } from '@sveltejs/kit'; import { prisma } from '../prisma'; -import { createClerkClient } from '@clerk/express'; +import { createClerkClient } from '@clerk/backend'; import { CLERK_SECRET_KEY } from '$env/static/private'; import { clerkClient } from 'clerk-sveltekit/server'; +import { logger } from '$lib/server/logger'; const clerkSessionClient = createClerkClient({ secretKey: CLERK_SECRET_KEY, }); export async function validateSession({ locals }: ServerLoadEvent) { - if (!locals.auth.userId) { + if (!locals.auth.userId || !locals.auth.sessionId) { return redirect(307, '/login'); } @@ -28,13 +29,24 @@ export async function validateSession({ locals }: ServerLoadEvent) { }); if (!user) { + if (clerkUser.emailAddresses.length === 0) { + logger.error('User has no email address'); + await clerkSessionClient.sessions.revokeSession(locals.auth.sessionId); + + return redirect(307, '/login'); + } + user = await prisma.user.create({ data: { clerkId: clerkUser.id, email: clerkUser.emailAddresses[0].emailAddress, - name: clerkUser.fullName ?? 'Ben the Man', + name: clerkUser.fullName ?? '', }, }); + + if (clerkUser.fullName === null) { + logger.error('User has no name'); + } } return { diff --git a/src/routes/app/+layout.svelte b/src/routes/app/+layout.svelte index 0a52b31..a01f36b 100644 --- a/src/routes/app/+layout.svelte +++ b/src/routes/app/+layout.svelte @@ -26,6 +26,9 @@ return; } + /** + * Set the active organization to the first one in the list, this is temporary until we let the user choose the organization + */ try { // Set the active organization to the first one in the list await clerk.setActive({