From c7e9318b6ac588d8dc6413d5e2c646f33d175fb7 Mon Sep 17 00:00:00 2001 From: David Haukeness Date: Tue, 5 Nov 2024 23:22:28 +0000 Subject: [PATCH] Add js-sha256 for Gravatar URL generation; refactor UserMenu to use email for avatar; update setup schema to remove username --- messages/en.json | 2 +- package-lock.json | 9 ++++++++- package.json | 3 ++- src/lib/components/custom/UserMenu.svelte | 17 ++++++++++++----- src/lib/server/db/index.ts | 13 ------------- src/routes/setup/+page.server.ts | 2 +- src/routes/setup/schema.ts | 8 +------- 7 files changed, 25 insertions(+), 29 deletions(-) diff --git a/messages/en.json b/messages/en.json index b868aa3..b494ce3 100644 --- a/messages/en.json +++ b/messages/en.json @@ -54,4 +54,4 @@ "title": "WireGuard Web", "toggleTheme": "Toggle Theme", "users": "Users" -} +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index a849579..5d1a86d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,7 +14,8 @@ "@oslojs/encoding": "^1.1.0", "@sveltejs/adapter-node": "^5.2.9", "better-sqlite3": "^11.1.2", - "drizzle-orm": "^0.33.0" + "drizzle-orm": "^0.33.0", + "js-sha256": "^0.11.0" }, "devDependencies": { "@sveltejs/kit": "^2.0.0", @@ -5532,6 +5533,12 @@ "@sideway/pinpoint": "^2.0.0" } }, + "node_modules/js-sha256": { + "version": "0.11.0", + "resolved": "https://registry.npmjs.org/js-sha256/-/js-sha256-0.11.0.tgz", + "integrity": "sha512-6xNlKayMZvds9h1Y1VWc0fQHQ82BxTXizWPEtEeGvmOUYpBRy4gbWroHLpzowe6xiQhHpelCQiE7HEdznyBL9Q==", + "license": "MIT" + }, "node_modules/js-yaml": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", diff --git a/package.json b/package.json index ab5ff1b..b46b369 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,7 @@ "@oslojs/encoding": "^1.1.0", "@sveltejs/adapter-node": "^5.2.9", "better-sqlite3": "^11.1.2", - "drizzle-orm": "^0.33.0" + "drizzle-orm": "^0.33.0", + "js-sha256": "^0.11.0" } } diff --git a/src/lib/components/custom/UserMenu.svelte b/src/lib/components/custom/UserMenu.svelte index 8997fde..e673fd9 100644 --- a/src/lib/components/custom/UserMenu.svelte +++ b/src/lib/components/custom/UserMenu.svelte @@ -2,19 +2,26 @@ import * as DropdownMenu from '$lib/components/ui/dropdown-menu'; import * as Avatar from '$lib/components/ui/avatar'; import * as m from '$lib/paraglide/messages'; - import type { UserInfo } from '$lib/types'; + import type { User } from '$lib/server/db'; + import { sha256 } from 'js-sha256'; import { goto } from "$app/navigation"; - import { LogOut, User, Users, RectangleEllipsis, Sun, Moon, House } from 'lucide-svelte'; + import { LogOut, User as UserIcon, Users, RectangleEllipsis, Sun, Moon, House } from 'lucide-svelte'; import { toggleMode } from 'mode-watcher'; - let { user }: { user: UserInfo | null } = $props(); + let { user }: { user: User | null } = $props(); + + const gravatarURL = (email: string): string => { + if (!email) return `https://www.gravatar.com/avatar/0000?d=404`; + const hash = sha256.create().update(email.trim().toLowerCase()).hex(); + return `https://www.gravatar.com/avatar/${hash}?d=404` + }; {#if user} - + {user.firstname?.charAt(0).toUpperCase() + user.lastname?.charAt(0).toUpperCase()} @@ -27,7 +34,7 @@ {m.myAccount()} goto('/profile')}> - + {m.profile()} goto('/password')}> diff --git a/src/lib/server/db/index.ts b/src/lib/server/db/index.ts index 747e47b..1f0d4e4 100644 --- a/src/lib/server/db/index.ts +++ b/src/lib/server/db/index.ts @@ -1,21 +1,8 @@ import { drizzle } from 'drizzle-orm/better-sqlite3'; -import { createHash } from 'crypto'; import Database from 'better-sqlite3'; const client = new Database( 'db.sqlite' ); export const db = drizzle(client); -/** - * Generates a Gravatar URL for the given email. - * - * @param email - The email address to generate the Gravatar URL for. If null, the function returns undefined. - * @returns The Gravatar URL for the given email, or undefined if the email is null. - */ -export const createGravatarURL = (email: string | null): string | undefined => { - if (!email) return undefined; - const hash = createHash('sha256').update(email.trim().toLowerCase()).digest('hex'); - return `https://www.gravatar.com/avatar/${hash}?d=404`; -}; - /** * Checks if an email exists in the users table. * diff --git a/src/routes/setup/+page.server.ts b/src/routes/setup/+page.server.ts index 1dcc7d7..a852b99 100644 --- a/src/routes/setup/+page.server.ts +++ b/src/routes/setup/+page.server.ts @@ -43,8 +43,8 @@ export const actions: Actions = { firstname: form.data.firstname, lastname: form.data.lastname, email: form.data.email, - username: form.data.username, password: hash, + last_login: new Date(Date.now()), }).returning() newId = result[0].id; } catch (e) { diff --git a/src/routes/setup/schema.ts b/src/routes/setup/schema.ts index 26dbfa3..628c10e 100644 --- a/src/routes/setup/schema.ts +++ b/src/routes/setup/schema.ts @@ -4,13 +4,7 @@ export const setupFormSchema = z.object({ firstname: z.string(), lastname: z.string(), email: z.string() - .email('Invalid email address') - .max(255, 'Email must be at most 255 characters long') - .optional(), - username: z.string() - .min(5, 'Must be at least 5 characters long') - .max(20, 'Username must be at most 20 characters long') - .regex(/^[a-zA-Z0-9_]+$/, 'Username must only contain letters, numbers, and underscores'), + .email('Invalid email address'), password: z.string() .min(8, 'Password must be at least 8 characters long') .max(255, 'Password must be at most 255 characters long')