From ec339cfd99d024c77d5315132cc6836da7c7c93b Mon Sep 17 00:00:00 2001 From: Izan Gil <66965250+SrIzan10@users.noreply.github.com> Date: Wed, 7 Aug 2024 23:20:13 +0200 Subject: [PATCH 1/7] feat: first commit, basically gravatar selector left --- compose.yml | 12 + drizzle.config.ts | 15 + package.json | 22 +- .../20240330233911_init/migration.sql | 10 - prisma/migrations/migration_lock.toml | 3 - prisma/schema.prisma | 22 - src/app.d.ts | 5 +- src/app.html | 11 +- src/drizzle/0000_wealthy_dagger.sql | 32 + src/drizzle/meta/0000_snapshot.json | 150 ++ src/drizzle/meta/_journal.json | 13 + src/hooks.client.ts | 23 - src/hooks.server.ts | 42 +- src/lib/components/NavBar.svelte | 57 +- src/lib/components/dashboard/Messages.svelte | 5 +- src/lib/components/dashboard/UserCard.svelte | 9 +- src/lib/server/auth/index.ts | 35 + src/lib/server/db/index.ts | 17 +- src/lib/server/db/schema.ts | 29 + src/lib/types/ResultType.ts | 23 + src/lib/zod/auth.ts | 6 + src/routes/+layout.server.ts | 7 + src/routes/+layout.svelte | 8 +- src/routes/+page.svelte | 12 + .../{[...user] => [user]}/+page.server.ts | 21 +- src/routes/{[...user] => [user]}/+page.svelte | 8 +- src/routes/api/logout/+server.ts | 15 + src/routes/dashboard/+page.server.ts | 20 +- src/routes/dashboard/+page.svelte | 14 +- src/routes/login/+page.server.ts | 47 + src/routes/login/+page.svelte | 39 + src/routes/register/+page.server.ts | 47 + src/routes/register/+page.svelte | 39 + src/routes/signIn/+page.svelte | 16 - src/routes/signUp/+page.svelte | 7 - static/android-chrome-192x192.png | Bin 0 -> 2025 bytes static/android-chrome-256x256.png | Bin 0 -> 2526 bytes static/apple-touch-icon.png | Bin 0 -> 1520 bytes static/browserconfig.xml | 9 + static/favicon-16x16.png | Bin 0 -> 450 bytes static/favicon-32x32.png | Bin 0 -> 639 bytes static/favicon.ico | Bin 0 -> 15086 bytes static/favicon.png | Bin 1571 -> 0 bytes static/html_code.html | 9 + static/mstile-150x150.png | Bin 0 -> 1533 bytes static/safari-pinned-tab.svg | 24 + static/site.webmanifest | 19 + static/tbh.png | Bin 0 -> 6746 bytes static/tbh.webp | Bin 0 -> 3742 bytes vite.config.ts | 5 +- yarn.lock | 1395 ++++++++--------- 51 files changed, 1385 insertions(+), 917 deletions(-) create mode 100644 compose.yml create mode 100644 drizzle.config.ts delete mode 100644 prisma/migrations/20240330233911_init/migration.sql delete mode 100644 prisma/migrations/migration_lock.toml delete mode 100644 prisma/schema.prisma create mode 100644 src/drizzle/0000_wealthy_dagger.sql create mode 100644 src/drizzle/meta/0000_snapshot.json create mode 100644 src/drizzle/meta/_journal.json delete mode 100644 src/hooks.client.ts create mode 100644 src/lib/server/auth/index.ts create mode 100644 src/lib/server/db/schema.ts create mode 100644 src/lib/types/ResultType.ts create mode 100644 src/lib/zod/auth.ts create mode 100644 src/routes/+layout.server.ts rename src/routes/{[...user] => [user]}/+page.server.ts (59%) rename src/routes/{[...user] => [user]}/+page.svelte (93%) create mode 100644 src/routes/api/logout/+server.ts create mode 100644 src/routes/login/+page.server.ts create mode 100644 src/routes/login/+page.svelte create mode 100644 src/routes/register/+page.server.ts create mode 100644 src/routes/register/+page.svelte delete mode 100644 src/routes/signIn/+page.svelte delete mode 100644 src/routes/signUp/+page.svelte create mode 100644 static/android-chrome-192x192.png create mode 100644 static/android-chrome-256x256.png create mode 100644 static/apple-touch-icon.png create mode 100644 static/browserconfig.xml create mode 100644 static/favicon-16x16.png create mode 100644 static/favicon-32x32.png create mode 100644 static/favicon.ico delete mode 100644 static/favicon.png create mode 100644 static/html_code.html create mode 100644 static/mstile-150x150.png create mode 100644 static/safari-pinned-tab.svg create mode 100644 static/site.webmanifest create mode 100644 static/tbh.png create mode 100644 static/tbh.webp diff --git a/compose.yml b/compose.yml new file mode 100644 index 0000000..d5ecbb7 --- /dev/null +++ b/compose.yml @@ -0,0 +1,12 @@ +volumes: + psql-tbh: +services: + psql: + image: postgres + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: dfsjhkdswkjntelsmldbfvsgknl5t + volumes: + - psql-tbh:/var/lib/postgresql/data + ports: + - 5555:5432 \ No newline at end of file diff --git a/drizzle.config.ts b/drizzle.config.ts new file mode 100644 index 0000000..9a7ef11 --- /dev/null +++ b/drizzle.config.ts @@ -0,0 +1,15 @@ +import 'dotenv/config'; +import { defineConfig } from 'drizzle-kit'; + +export default defineConfig({ + dialect: 'postgresql', + out: './src/drizzle', + schema: './src/lib/server/db/schema.ts', + dbCredentials: { + url: process.env.DATABASE_URL!, + }, + // Print all statements + verbose: true, + // Always ask for confirmation + strict: true, +}); \ No newline at end of file diff --git a/package.json b/package.json index 404591f..7223b12 100644 --- a/package.json +++ b/package.json @@ -2,16 +2,13 @@ "name": "tbh", "version": "0.0.1-alpha", "private": true, - "packageManager": "yarn@1.22.21", "scripts": { "dev": "vite dev", "dev:pages": "wrangler pages dev .svelte-kit/cloudflare", - "build": "yarn db:generateprod && vite build", + "build": "vite build", "preview": "vite preview", "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json", "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch", - "db:generate": "prisma generate", - "db:generateprod": "prisma generate --no-engine", "tail": "wrangler pages deployment tail --project-name=tbh" }, "devDependencies": { @@ -22,6 +19,8 @@ "@types/ws": "^8.5.10", "autoprefixer": "^10.4.17", "daisyui": "^4.6.1", + "dotenv": "^16.4.5", + "drizzle-kit": "^0.23.2", "postcss": "^8.4.34", "prisma": "^5.11.0", "svelte": "^4.2.7", @@ -34,16 +33,21 @@ }, "type": "module", "dependencies": { - "@clerk/themes": "^1.7.10", + "@libsql/client": "^0.6.1", + "@lucia-auth/adapter-drizzle": "^1.0.7", "@neondatabase/serverless": "^0.9.0", "@prisma/adapter-neon": "^5.11.0", "@prisma/client": "^5.11.0", "@prisma/extension-accelerate": "^1.0.0", "@types/node": "^20.11.17", - "clerk-sveltekit": "^0.4.1", - "isomorphic-ws": "^5.0.0", + "drizzle-orm": "^0.32.2", + "lucia": "^3.2.0", + "lucide-svelte": "^0.381.0", + "postgres": "^3.4.4", + "svelte-sonner": "^0.3.27", "ts-node": "^10.9.2", "ws": "^8.16.0", - "zod": "^3.22.4" - } + "zod": "^3.23.8" + }, + "packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e" } diff --git a/prisma/migrations/20240330233911_init/migration.sql b/prisma/migrations/20240330233911_init/migration.sql deleted file mode 100644 index 977c674..0000000 --- a/prisma/migrations/20240330233911_init/migration.sql +++ /dev/null @@ -1,10 +0,0 @@ --- CreateTable -CREATE TABLE "Question" ( - "id" TEXT NOT NULL, - "body" TEXT NOT NULL, - "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updatedAt" TIMESTAMP(3) NOT NULL, - "forUser" TEXT NOT NULL, - - CONSTRAINT "Question_pkey" PRIMARY KEY ("id") -); diff --git a/prisma/migrations/migration_lock.toml b/prisma/migrations/migration_lock.toml deleted file mode 100644 index fbffa92..0000000 --- a/prisma/migrations/migration_lock.toml +++ /dev/null @@ -1,3 +0,0 @@ -# Please do not edit this file manually -# It should be added in your version-control system (i.e. Git) -provider = "postgresql" \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma deleted file mode 100644 index d0a5fcf..0000000 --- a/prisma/schema.prisma +++ /dev/null @@ -1,22 +0,0 @@ -// This is your Prisma schema file, -// learn more about it in the docs: https://pris.ly/d/prisma-schema - -// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions? -// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init - -generator client { - provider = "prisma-client-js" -} - -datasource db { - provider = "postgresql" - url = env("DATABASE_URL") -} - -model Question { - id String @id @default(uuid()) - body String - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt - forUser String -} \ No newline at end of file diff --git a/src/app.d.ts b/src/app.d.ts index 743f07b..fc92ec8 100644 --- a/src/app.d.ts +++ b/src/app.d.ts @@ -3,7 +3,10 @@ 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/app.html b/src/app.html index fdf2de7..39e70d7 100644 --- a/src/app.html +++ b/src/app.html @@ -2,10 +2,19 @@
- %sveltekit.head% + + + + + + + + + +bottom text
diff --git a/src/routes/[...user]/+page.server.ts b/src/routes/[user]/+page.server.ts similarity index 59% rename from src/routes/[...user]/+page.server.ts rename to src/routes/[user]/+page.server.ts index 32cbd8a..4464bcb 100644 --- a/src/routes/[...user]/+page.server.ts +++ b/src/routes/[user]/+page.server.ts @@ -1,17 +1,17 @@ -import { CLERK_SECRET_KEY } from '$env/static/private'; import type { PageServerLoad } from './$types'; import { z } from 'zod'; import { redirect } from '@sveltejs/kit'; -import type { User } from '$lib/types/User'; -import prisma from '$lib/server/db'; +import db from '$lib/server/db'; +import * as schema from '$lib/server/db/schema'; +import { eq } from 'drizzle-orm'; export const load: PageServerLoad = async ({ params }) => { - const getUser = (await fetch(`https://api.clerk.com/v1/users?username=${params.user}`, { headers: { 'Authorization': `Bearer ${CLERK_SECRET_KEY}` } }).then(res => res.json()))[0] as User | null; + const getUser = await db.select().from(schema.user).where(eq(schema.user.username, params.user)).execute().then(res => res[0]); if (!getUser) throw redirect(302, '/404') return { - imageUrl: getUser.image_url, - username: getUser.username!, + imageUrl: getUser.imageUrl, + username: getUser.username, }; }; @@ -28,11 +28,10 @@ export const actions = { try { formSchema.parse({ question, username }) - await prisma.question.create({ - data: { - body: question as string, - forUser: username as string - } + await db.insert(schema.question).values({ + body: question as string, + forUsername: username as string, + id: crypto.randomUUID() }) return { success: true, diff --git a/src/routes/[...user]/+page.svelte b/src/routes/[user]/+page.svelte similarity index 93% rename from src/routes/[...user]/+page.svelte rename to src/routes/[user]/+page.svelte index cd488e1..88cb62b 100644 --- a/src/routes/[...user]/+page.svelte +++ b/src/routes/[user]/+page.svelte @@ -1,10 +1,10 @@ {#if form} diff --git a/src/routes/api/logout/+server.ts b/src/routes/api/logout/+server.ts new file mode 100644 index 0000000..24fcd6d --- /dev/null +++ b/src/routes/api/logout/+server.ts @@ -0,0 +1,15 @@ +import { error, redirect } from '@sveltejs/kit'; +import { lucia } from '$lib/server/auth'; + +export const GET = async ({ locals, cookies }) => { + if (!locals.session) return error(401, 'Unauthorized'); + + await lucia.invalidateSession(locals.session.id) + + const sessionCookie = lucia.createBlankSessionCookie(); + cookies.set(sessionCookie.name, sessionCookie.value, { + path: '.', + ...sessionCookie.attributes + }); + return redirect(302, '/') +}; \ No newline at end of file diff --git a/src/routes/dashboard/+page.server.ts b/src/routes/dashboard/+page.server.ts index e591d3d..2a51623 100644 --- a/src/routes/dashboard/+page.server.ts +++ b/src/routes/dashboard/+page.server.ts @@ -1,19 +1,17 @@ -import { CLERK_SECRET_KEY } from '$env/static/private'; -import prisma from '$lib/server/db'; -import type { User } from '$lib/types/User'; +import db from '$lib/server/db'; +import { desc, eq } from 'drizzle-orm'; import type { PageServerLoad } from './$types'; +import { question } from '$lib/server/db/schema'; +import { redirect } from '@sveltejs/kit'; export const load: PageServerLoad = async (event) => { // @ts-ignore - const user = event.locals.session; - const getUsername = (await fetch(`https://api.clerk.com/v1/users?user_id=${user.userId}`, { headers: { 'Authorization': `Bearer ${CLERK_SECRET_KEY}` } }).then(res => res.json()))[0] as User; - const questions = await prisma.question.findMany({ - where: { - forUser: getUsername.username! - } - }) + const user = event.locals.user; + if (!user) return redirect(302, '/login') + const questions = await db.select().from(question).where(eq(question.forUsername, user.username)).orderBy(desc(question.createdAt)).execute(); return { - questions + questions, + user } }; \ No newline at end of file diff --git a/src/routes/dashboard/+page.svelte b/src/routes/dashboard/+page.svelte index 3e1e309..09c01db 100644 --- a/src/routes/dashboard/+page.svelte +++ b/src/routes/dashboard/+page.svelte @@ -1,19 +1,13 @@ -Loading!
-It's great to see you again!
+Let's start your anonymous question page!
+Cooking...
-zTkbY?dx;eUPod?keWYaYLYGLq^WXJm{)4V%P|M)s)XTJL{Ot}A;n
zX22Gh2ORM{*?#8M`Tfqgqy6INQ~_2x7Sw#5@cCtKoM!zxnSN7|eS0k)e*_`uTrZ)~
z_2}PqaW4eKgjG9#125Ow%zhut{TZ~}!3+V>ZzONy6EB3CSZG4`ku1@c!s0(J8E5dz
zTuU6RaLu|e*lX~{u2d^M$Gh8VsHQ;ivo8nY5~`#Lj~;Fzptl!uDKK3|857?Z%UxoK
z11%UOOhvR4x1Fez<@g1j*uYjR`(PwKI!ufERz8F==CQ94bi%$>-&0}HBvFFM*e>R_
zLDGgptF@gIO?D4N%jvjmhf?#CS(!1Z7k4OnP_1|Ba%o0$GTGFI)og3-sOTw?9<@!v
z(e2(ZP}H^0?I@ 6h%bi*
md%OF)Rl6l#avZrw^ues6Y|N7N!OOcVfRmkzZId Auj;SaFF$CF%mC(Td{R6_CEEo0wDM{yUSZL#
z!YuMaf3}B8>oj(>dX+>oM%P6DR#y6Dqx0jD6Z%c{%`lmat!TA3xyz{)eW{FML7Oi>
zm#7<||5s7R{^+h1eQGx1eQlzt*Avo+?TttP6dGy7zC(}z0^$RPG}^a*boBrzbZ9nW
z-`C|U(Ei!IWef|9{ci|#2WX4%-$ulH52(@8rb8BylPx#Ay7
aB^>EX>4U6ba`-PAZc)PV*mhnoa6Eg2ys>@
zD9TUE%t_@^00ScnE@KN5BNI!L6ay0=M1VBIWCJ6!R3OXP)X2ol#2my2%YaCrN-hBE
W7ZG&wLN%2D0000