From a6c6b4a7a7f33c1303b818fa5dc964c2b444b714 Mon Sep 17 00:00:00 2001 From: Dan Mihailescu Date: Sun, 15 Dec 2024 17:29:28 -0500 Subject: [PATCH] Lucia register done --- prisma/dev.db | Bin 32768 -> 49152 bytes .../migration.sql | 41 +++++++++++++++ .../migration.sql | 23 +++++++++ .../migration.sql | 22 ++++++++ .../migration.sql | 47 ++++++++++++++++++ .../migration.sql | 24 +++++++++ prisma/schema.prisma | 13 +++-- src/app.d.ts | 6 ++- src/lib/server/lucia.ts | 2 +- src/routes/login/+page.server.ts | 15 ++++-- 10 files changed, 181 insertions(+), 12 deletions(-) create mode 100644 prisma/migrations/20241215210334_added_password/migration.sql create mode 100644 prisma/migrations/20241215213842_removed_issue/migration.sql create mode 100644 prisma/migrations/20241215221739_removed_issue/migration.sql create mode 100644 prisma/migrations/20241215222117_removed_issue/migration.sql create mode 100644 prisma/migrations/20241215222709_removed_issue/migration.sql diff --git a/prisma/dev.db b/prisma/dev.db index c20c2612b2b83c3ed65cb4d96908a4898dc63a34..5a0a90c9b305e1442a25ae98dd31bd62897591f3 100644 GIT binary patch literal 49152 zcmeI*+i%;}831s}vaQ&Tt#+NGtyus;&e|9bTOM8xNwh(#%4(v_krQ3oxIrMCb9ksI z)-9qG+Zl#J&6;k&-nQ314MQIXY;PU*AM9}`2J~qi1`HTbpum8xZ~I6|u^l^$yGzlu z9}UtLd3gBocP_k0qDxoT>OGnA+TAAWrS!;Ck!UpXN-7nJL{?xt4&xk*Q5c_taT3Py z5X^hruo8LU&QIoNT9MiK=aKmj6TdzFY5eoK)wz%6el!2p%v*Cmo!fa_7PuS*PyhwK zb^`al^;A5$v=n`3v&VQtw&Yf|Bz?c$ZteMVE?dlH%DGfI^XghImEN-?ojNm{f+1aZ z)2V!+oV$=KrV8ujRH3rAmby~RU(OUaQa{LTq%xKAdcFV|UCtHCXNN1@t3DG?o;nqM z_vr^4ztZ-5LH@z-1Dm~9_->f~e*4h$vj@Wrve_47Id=_$J0!qSN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pQT+(+H?=aN0~bIKRecgqjXeUwuT?@XxjKk|6$h27efdd;d| zxw=)G3Z>tumsHVaNh>`noGEo!TU4!j?Rvd9{=r5roKf zLgqPy$Ak*cLCoQim&sCxdO;ksNJ_tc^}`zuatL-XP8(+z23cZfXi5_fH|OvWMjl75 zO?lI|2|~UdMsCJI-6kat7MOb?_FXq%u7gR)Tuy|D2+24waVbJ1jGd5s0VmXrLoP@& z@F)vWgq?s97UMulzY56HImdDaf--5JqP?V(^d~3t#cYCPco)DQ;oYoNU&0&bm4@^r z9g@!}jVCH$YVjDrKjGu?+|e)ajH*bIEGvp(m=pU)0R90VWoM_bT9OrARw`2d?6M{) z6(b+Z>AeAf$8eBg_TXSkE`W_i*X69U)=u8Y=0NnC2lVpm`ADoayY1cWmqhQNmzQp1 zCu^#OgNu}2B>sP-uv7r+tEw7_&YP^AbT0Qrub-=2{X1dpu-46P{;H?<@_pv?-n6b` z0DpzY8Pm7$`6XpT`d=P-URgK(rJolj!G{1o%p|^p_H&XBo?X!mRee#kT3zey?tY6e WZ*})Y+q!nEchJtvuUlVC_rCxM?G{o1 diff --git a/prisma/migrations/20241215210334_added_password/migration.sql b/prisma/migrations/20241215210334_added_password/migration.sql new file mode 100644 index 0000000..be6f695 --- /dev/null +++ b/prisma/migrations/20241215210334_added_password/migration.sql @@ -0,0 +1,41 @@ +/* + Warnings: + + - Added the required column `password` to the `User` table without a default value. This is not possible if the table is not empty. + +*/ +-- CreateTable +CREATE TABLE "Session" ( + "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + "expiresAt" DATETIME NOT NULL, + "sessionToken" TEXT NOT NULL, + "accessToken" TEXT NOT NULL, + "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" DATETIME NOT NULL, + "userId" INTEGER NOT NULL, + CONSTRAINT "Session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE +); + +-- RedefineTables +PRAGMA defer_foreign_keys=ON; +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_User" ( + "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + "email" TEXT, + "name" TEXT NOT NULL, + "password" TEXT NOT NULL, + "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP +); +INSERT INTO "new_User" ("createdAt", "email", "id", "name", "updatedAt") SELECT "createdAt", "email", "id", "name", "updatedAt" FROM "User"; +DROP TABLE "User"; +ALTER TABLE "new_User" RENAME TO "User"; +CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); +PRAGMA foreign_keys=ON; +PRAGMA defer_foreign_keys=OFF; + +-- CreateIndex +CREATE UNIQUE INDEX "Session_sessionToken_key" ON "Session"("sessionToken"); + +-- CreateIndex +CREATE UNIQUE INDEX "Session_accessToken_key" ON "Session"("accessToken"); diff --git a/prisma/migrations/20241215213842_removed_issue/migration.sql b/prisma/migrations/20241215213842_removed_issue/migration.sql new file mode 100644 index 0000000..e3d5fe7 --- /dev/null +++ b/prisma/migrations/20241215213842_removed_issue/migration.sql @@ -0,0 +1,23 @@ +/* + Warnings: + + - You are about to drop the column `accessToken` on the `Session` table. All the data in the column will be lost. + - You are about to drop the column `sessionToken` on the `Session` table. All the data in the column will be lost. + +*/ +-- RedefineTables +PRAGMA defer_foreign_keys=ON; +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_Session" ( + "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + "expiresAt" DATETIME NOT NULL, + "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" DATETIME NOT NULL, + "userId" INTEGER NOT NULL, + CONSTRAINT "Session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE +); +INSERT INTO "new_Session" ("createdAt", "expiresAt", "id", "updatedAt", "userId") SELECT "createdAt", "expiresAt", "id", "updatedAt", "userId" FROM "Session"; +DROP TABLE "Session"; +ALTER TABLE "new_Session" RENAME TO "Session"; +PRAGMA foreign_keys=ON; +PRAGMA defer_foreign_keys=OFF; diff --git a/prisma/migrations/20241215221739_removed_issue/migration.sql b/prisma/migrations/20241215221739_removed_issue/migration.sql new file mode 100644 index 0000000..fffc584 --- /dev/null +++ b/prisma/migrations/20241215221739_removed_issue/migration.sql @@ -0,0 +1,22 @@ +/* + Warnings: + + - The primary key for the `Session` table will be changed. If it partially fails, the table could be left without primary key constraint. + +*/ +-- RedefineTables +PRAGMA defer_foreign_keys=ON; +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_Session" ( + "id" TEXT NOT NULL PRIMARY KEY, + "expiresAt" DATETIME NOT NULL, + "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" DATETIME NOT NULL, + "userId" INTEGER NOT NULL, + CONSTRAINT "Session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE +); +INSERT INTO "new_Session" ("createdAt", "expiresAt", "id", "updatedAt", "userId") SELECT "createdAt", "expiresAt", "id", "updatedAt", "userId" FROM "Session"; +DROP TABLE "Session"; +ALTER TABLE "new_Session" RENAME TO "Session"; +PRAGMA foreign_keys=ON; +PRAGMA defer_foreign_keys=OFF; diff --git a/prisma/migrations/20241215222117_removed_issue/migration.sql b/prisma/migrations/20241215222117_removed_issue/migration.sql new file mode 100644 index 0000000..1bc57ba --- /dev/null +++ b/prisma/migrations/20241215222117_removed_issue/migration.sql @@ -0,0 +1,47 @@ +/* + Warnings: + + - The primary key for the `User` table will be changed. If it partially fails, the table could be left without primary key constraint. + +*/ +-- RedefineTables +PRAGMA defer_foreign_keys=ON; +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_Post" ( + "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + "title" TEXT NOT NULL, + "content" TEXT NOT NULL, + "published" BOOLEAN DEFAULT false, + "authorId" TEXT NOT NULL, + "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT "Post_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE +); +INSERT INTO "new_Post" ("authorId", "content", "createdAt", "id", "published", "title", "updatedAt") SELECT "authorId", "content", "createdAt", "id", "published", "title", "updatedAt" FROM "Post"; +DROP TABLE "Post"; +ALTER TABLE "new_Post" RENAME TO "Post"; +CREATE TABLE "new_Session" ( + "id" TEXT NOT NULL PRIMARY KEY, + "expiresAt" DATETIME NOT NULL, + "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" DATETIME NOT NULL, + "userId" TEXT NOT NULL, + CONSTRAINT "Session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE +); +INSERT INTO "new_Session" ("createdAt", "expiresAt", "id", "updatedAt", "userId") SELECT "createdAt", "expiresAt", "id", "updatedAt", "userId" FROM "Session"; +DROP TABLE "Session"; +ALTER TABLE "new_Session" RENAME TO "Session"; +CREATE TABLE "new_User" ( + "id" TEXT NOT NULL PRIMARY KEY, + "email" TEXT, + "name" TEXT NOT NULL, + "password" TEXT NOT NULL, + "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP +); +INSERT INTO "new_User" ("createdAt", "email", "id", "name", "password", "updatedAt") SELECT "createdAt", "email", "id", "name", "password", "updatedAt" FROM "User"; +DROP TABLE "User"; +ALTER TABLE "new_User" RENAME TO "User"; +CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); +PRAGMA foreign_keys=ON; +PRAGMA defer_foreign_keys=OFF; diff --git a/prisma/migrations/20241215222709_removed_issue/migration.sql b/prisma/migrations/20241215222709_removed_issue/migration.sql new file mode 100644 index 0000000..1aada7c --- /dev/null +++ b/prisma/migrations/20241215222709_removed_issue/migration.sql @@ -0,0 +1,24 @@ +/* + Warnings: + + - The primary key for the `Post` table will be changed. If it partially fails, the table could be left without primary key constraint. + +*/ +-- RedefineTables +PRAGMA defer_foreign_keys=ON; +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_Post" ( + "id" TEXT NOT NULL PRIMARY KEY, + "title" TEXT NOT NULL, + "content" TEXT NOT NULL, + "published" BOOLEAN DEFAULT false, + "authorId" TEXT NOT NULL, + "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT "Post_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE +); +INSERT INTO "new_Post" ("authorId", "content", "createdAt", "id", "published", "title", "updatedAt") SELECT "authorId", "content", "createdAt", "id", "published", "title", "updatedAt" FROM "Post"; +DROP TABLE "Post"; +ALTER TABLE "new_Post" RENAME TO "Post"; +PRAGMA foreign_keys=ON; +PRAGMA defer_foreign_keys=OFF; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 6369b5f..0192fdb 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -15,9 +15,10 @@ datasource db { } model User { - id Int @id @default(autoincrement()) + id String @id @default(uuid()) email String? @unique name String + password String posts Post[] sessions Session[] @@ -26,24 +27,22 @@ model User { } model Session { - id Int @id @default(autoincrement()) + id String @id @default(uuid()) expiresAt DateTime - sessionToken String @unique - accessToken String @unique createdAt DateTime @default(now()) updatedAt DateTime @updatedAt - userId Int + userId String user User @relation(references: [id], fields: [userId]) } model Post { - id Int @id @default(autoincrement()) + id String @id @default(uuid()) title String content String published Boolean? @default(false) author User @relation(fields: [authorId], references: [id]) - authorId Int + authorId String createdAt DateTime @default(now()) updatedAt DateTime @default(now()) @updatedAt } \ No newline at end of file diff --git a/src/app.d.ts b/src/app.d.ts index da08e6d..bb055d9 100644 --- a/src/app.d.ts +++ b/src/app.d.ts @@ -2,8 +2,12 @@ // for information about these interfaces declare global { namespace App { + // interface Error {} - // interface Locals {} + interface Locals { + user: import("lucia").User | null; + session: import('lucia').Session | null; + } // interface PageData {} // interface PageState {} // interface Platform {} diff --git a/src/lib/server/lucia.ts b/src/lib/server/lucia.ts index bb5cda7..f93f932 100644 --- a/src/lib/server/lucia.ts +++ b/src/lib/server/lucia.ts @@ -8,7 +8,7 @@ const adapter = new PrismaAdapter(client.session, client.user) export const auth = new Lucia(adapter, { sessionCookie: { attributes: { -// secure: process.env.NODE_ENV === "production" + secure: process.env.NODE_ENV === "production" } }, getUserAttributes: (attributes)=>{ diff --git a/src/routes/login/+page.server.ts b/src/routes/login/+page.server.ts index 5216540..b60409d 100644 --- a/src/routes/login/+page.server.ts +++ b/src/routes/login/+page.server.ts @@ -1,6 +1,10 @@ import { logger } from '$lib/server/logger'; import { prisma } from '$lib/server/prisma'; import { error, redirect, type Actions } from '@sveltejs/kit'; +//import { password } from 'bun'; +import { Argon2id } from "oslo/password" +import { generateId } from 'lucia'; +import { auth } from '$lib/server/lucia.js'; export const actions = { login: async (event) => { @@ -25,22 +29,27 @@ export const actions = { }, register: async (event) => { const form = await event.request.formData(); - if (!form.has('email') || !form.has('name')) { + if (!form.has('email') || !form.has('name') || !form.has('password')) { return error(400); } + const hashedPassword = await new Argon2id().hash(form.get('password') as string) const user = await prisma.user.create({ data: { email: form.get('email') as string, - name: form.get('name') as string + name: form.get('name') as string, + password: hashedPassword } }); + const session = await auth.createSession(user.id.toString(), {}); + const sessionCookie = auth.createSessionCookie(session.id); if (!user) { return error(500); } - event.cookies.set('user', String(user.id), { + event.cookies.set(sessionCookie.name, sessionCookie.value, { path: '/', maxAge: 120 }); redirect(302, '/'); + } } satisfies Actions; \ No newline at end of file