From 8c473576059f32d49727d52572d6787cd3ea5d7d Mon Sep 17 00:00:00 2001 From: Bradley Shellnut Date: Mon, 17 Jun 2024 17:37:47 -0700 Subject: [PATCH] Fixing a lot of check issues. --- src/lib/utils/gameMapper.ts | 4 +- src/lib/validations/auth.ts | 1 - .../(app)/(protected)/admin/+layout.server.ts | 8 +-- .../(protected)/admin/users/+page.server.ts | 4 +- .../collections/[id]/+page.server.ts | 7 +-- .../collections/add/+page.server.ts | 2 +- .../(app)/(protected)/list/+layout.server.ts | 13 +++-- .../(protected)/list/[id]/+page.server.ts | 3 +- .../(app)/(protected)/profile/+page.server.ts | 3 +- .../security/two-factor/+page.server.ts | 4 +- .../(protected)/wishlists/+page.server.ts | 10 ++-- .../wishlists/[id]/+page.server.ts | 12 ++-- src/routes/(app)/search/+page.server.ts | 56 ++++++++++--------- src/routes/(app)/search/+page.svelte | 15 +++-- src/routes/(auth)/login/+page.server.ts | 19 ------- src/routes/(auth)/login/+page.svelte | 30 +--------- src/routes/(auth)/sign-up/+page.server.ts | 2 + src/routes/(auth)/sign-up/+page.svelte | 4 +- src/routes/(auth)/totp/+page.server.ts | 16 +++--- src/routes/(auth)/totp/+page.svelte | 7 +-- src/routes/api/games/search/+server.ts | 2 - 21 files changed, 95 insertions(+), 127 deletions(-) diff --git a/src/lib/utils/gameMapper.ts b/src/lib/utils/gameMapper.ts index a2e8898..590abce 100644 --- a/src/lib/utils/gameMapper.ts +++ b/src/lib/utils/gameMapper.ts @@ -48,8 +48,8 @@ export function mapAPIGameToBoredGame(game: GameType): Games { return { name: game.name, slug: kebabCase(game.name), - thumb_url: game.thumbnail, - image_url: game.image, + thumb_url: game.thumb_url, + image_url: game.image_url, year_published: game.year_published, min_players: game.min_players, max_players: game.max_players, diff --git a/src/lib/validations/auth.ts b/src/lib/validations/auth.ts index bc9367c..d5af946 100644 --- a/src/lib/validations/auth.ts +++ b/src/lib/validations/auth.ts @@ -10,7 +10,6 @@ export const signUpSchema = userSchema username: true, password: true, confirm_password: true, - terms: true, }) .superRefine(({ confirm_password, password }, ctx) => { refinePasswords(confirm_password, password, ctx); diff --git a/src/routes/(app)/(protected)/admin/+layout.server.ts b/src/routes/(app)/(protected)/admin/+layout.server.ts index 0186128..5cb8b59 100644 --- a/src/routes/(app)/(protected)/admin/+layout.server.ts +++ b/src/routes/(app)/(protected)/admin/+layout.server.ts @@ -12,8 +12,8 @@ export const load = loadFlash(async (event) => { redirect(302, '/login', notSignedInMessage, event); } - const userRoles = await db.query.userRoles.findMany({ - where: eq(user_roles.user_id, user!.id!), + const dbUserRoles = await db.query.userRoles.findMany({ + where: eq(userRoles.user_id, user!.id!), with: { role: { columns: { @@ -23,8 +23,8 @@ export const load = loadFlash(async (event) => { }, }); - const containsAdminRole = userRoles.some((userRoles) => user_role?.role?.name === 'admin'); - if (!userRoles?.length || !containsAdminRole) { + const containsAdminRole = dbUserRoles.some((userRole) => userRole?.role?.name === 'admin'); + if (!dbUserRoles?.length || !containsAdminRole) { console.log('Not an admin'); redirect(302, '/', forbiddenMessage, event); } diff --git a/src/routes/(app)/(protected)/admin/users/+page.server.ts b/src/routes/(app)/(protected)/admin/users/+page.server.ts index 1e5dead..b6e7d5a 100644 --- a/src/routes/(app)/(protected)/admin/users/+page.server.ts +++ b/src/routes/(app)/(protected)/admin/users/+page.server.ts @@ -1,12 +1,14 @@ +import { redirect } from 'sveltekit-flash-message/server'; import type { PageServerLoad } from './$types'; import db from '../../../../../db'; import { userNotAuthenticated } from '$lib/server/auth-utils'; +import { notSignedInMessage } from '$lib/flashMessages'; export const load: PageServerLoad = async (event) => { const { locals } = event; const { user, session } = locals; if (userNotAuthenticated(user, session)) { - return fail(401); + redirect(302, '/login', notSignedInMessage, event); } const users = await db.query.users.findMany({ diff --git a/src/routes/(app)/(protected)/collections/[id]/+page.server.ts b/src/routes/(app)/(protected)/collections/[id]/+page.server.ts index 55335d5..b99d52c 100644 --- a/src/routes/(app)/(protected)/collections/[id]/+page.server.ts +++ b/src/routes/(app)/(protected)/collections/[id]/+page.server.ts @@ -39,7 +39,7 @@ export async function load(event) { cuid: true, name: true, }, - where: and(eq(collections.user_id, user.id), eq(collections.cuid, id)), + where: and(eq(collections.user_id, user!.id!), eq(collections.cuid, id)), }); console.log('collection', collection); @@ -110,7 +110,6 @@ export const actions: Actions = { const form = await superValidate(event, zod(modifyListGameSchema)); - const user = event.locals.user; const game = await db.query.games.findFirst({ where: eq(games.id, form.data.id), }); @@ -127,7 +126,7 @@ export const actions: Actions = { try { const collection = await db.query.collections.findFirst({ - where: eq(collections.user_id, user.id), + where: eq(collections.user_id, user!.id!), }); if (!collection) { @@ -187,7 +186,7 @@ export const actions: Actions = { try { const collection = await db.query.collections.findFirst({ - where: eq(collections.user_id, locals.user.id), + where: eq(collections.user_id, user!.id!), }); if (!collection) { diff --git a/src/routes/(app)/(protected)/collections/add/+page.server.ts b/src/routes/(app)/(protected)/collections/add/+page.server.ts index 40d590e..174fbbc 100644 --- a/src/routes/(app)/(protected)/collections/add/+page.server.ts +++ b/src/routes/(app)/(protected)/collections/add/+page.server.ts @@ -1,4 +1,4 @@ -import { redirect } from '@sveltejs/kit'; +import { redirect } from 'sveltekit-flash-message/server'; import { notSignedInMessage } from '$lib/flashMessages'; import { userNotAuthenticated } from '$lib/server/auth-utils'; diff --git a/src/routes/(app)/(protected)/list/+layout.server.ts b/src/routes/(app)/(protected)/list/+layout.server.ts index 3e0f3ad..f0aa34c 100644 --- a/src/routes/(app)/(protected)/list/+layout.server.ts +++ b/src/routes/(app)/(protected)/list/+layout.server.ts @@ -1,23 +1,24 @@ -import { fail } from '@sveltejs/kit'; +import { redirect } from 'sveltekit-flash-message/server'; import { eq } from 'drizzle-orm'; import db from '../../../../db'; import { wishlists } from '$db/schema'; import { userNotAuthenticated } from '$lib/server/auth-utils'; import { notSignedInMessage } from '$lib/flashMessages'; -export async function load({ locals }) { +export async function load(event) { + const { locals } = event; const { user, session } = locals; if (userNotAuthenticated(user, session)) { - throw fail(401); + redirect(302, '/login', notSignedInMessage, event); } try { - const userWishlists = await db.query.wishlists.findMany({ - where: eq(wishlists.user_id, locals.user.id), + const dbWishlists = await db.query.wishlists.findMany({ + where: eq(wishlists.user_id, user!.id!), }); return { - wishlsits: userWishlists, + wishlists: dbWishlists, }; } catch (e) { console.error(e); diff --git a/src/routes/(app)/(protected)/list/[id]/+page.server.ts b/src/routes/(app)/(protected)/list/[id]/+page.server.ts index a0bb620..ff59f4d 100644 --- a/src/routes/(app)/(protected)/list/[id]/+page.server.ts +++ b/src/routes/(app)/(protected)/list/[id]/+page.server.ts @@ -1,7 +1,8 @@ -import { type Actions, fail, redirect } from '@sveltejs/kit'; +import { type Actions, fail } from '@sveltejs/kit'; import { eq } from 'drizzle-orm'; import { zod } from 'sveltekit-superforms/adapters'; import { superValidate } from 'sveltekit-superforms/server'; +import { redirect } from 'sveltekit-flash-message/server'; import db from '../../../../../db'; import { modifyListGameSchema } from '$lib/validations/zod-schemas'; import { games, wishlist_items, wishlists } from '$db/schema'; diff --git a/src/routes/(app)/(protected)/profile/+page.server.ts b/src/routes/(app)/(protected)/profile/+page.server.ts index 3693831..5d0ff87 100644 --- a/src/routes/(app)/(protected)/profile/+page.server.ts +++ b/src/routes/(app)/(protected)/profile/+page.server.ts @@ -19,7 +19,7 @@ export const load: PageServerLoad = async (event) => { } const dbUser = await db.query.users.findFirst({ - where: eq(users.id, user.id), + where: eq(users.id, user!.id!), }); const profileForm = await superValidate(zod(profileSchema), { @@ -85,6 +85,7 @@ export const actions: Actions = { }) .where(eq(users.id, user.id)); } catch (e) { + // @ts-expect-error if (e.message === `AUTH_INVALID_USER_ID`) { // invalid user id console.error(e); diff --git a/src/routes/(app)/(protected)/profile/security/two-factor/+page.server.ts b/src/routes/(app)/(protected)/profile/security/two-factor/+page.server.ts index ca8c124..4b61e08 100644 --- a/src/routes/(app)/(protected)/profile/security/two-factor/+page.server.ts +++ b/src/routes/(app)/(protected)/profile/security/two-factor/+page.server.ts @@ -92,7 +92,7 @@ export const actions: Actions = { } const dbUser = await db.query.users.findFirst({ - where: eq(users.id, user.id), + where: eq(users.id, user!.id!), }); if (!dbUser?.hashed_password) { @@ -136,7 +136,7 @@ export const actions: Actions = { return setError(addTwoFactorForm, 'two_factor_code', 'Invalid code'); } - await db.update(users).set({ two_factor_enabled: true }).where(eq(users.id, user.id)); + await db.update(users).set({ two_factor_enabled: true }).where(eq(users.id, user!.id!)); redirect(302, '/profile/security/two-factor/recovery-codes'); }, diff --git a/src/routes/(app)/(protected)/wishlists/+page.server.ts b/src/routes/(app)/(protected)/wishlists/+page.server.ts index 8a086f8..9c60379 100644 --- a/src/routes/(app)/(protected)/wishlists/+page.server.ts +++ b/src/routes/(app)/(protected)/wishlists/+page.server.ts @@ -10,7 +10,7 @@ import { games, wishlist_items, wishlists } from '$db/schema'; import { userNotAuthenticated } from '$lib/server/auth-utils'; export async function load(event) { - const { params, locals } = event; + const { locals } = event; const { user, session } = locals; if (userNotAuthenticated(user, session)) { redirect(302, '/login', notSignedInMessage, event); @@ -22,7 +22,7 @@ export async function load(event) { name: true, created_at: true, }, - where: eq(wishlists.user_id, user.id), + where: eq(wishlists.user_id, user!.id!), }); console.log('wishlists', userWishlists); @@ -63,7 +63,7 @@ export const actions: Actions = { if (game) { const wishlist = await db.query.wishlists.findFirst({ - where: eq(wishlists.user_id, locals.user.id), + where: eq(wishlists.user_id, user!.id!), }); if (!wishlist) { @@ -105,7 +105,7 @@ export const actions: Actions = { }, // Remove game from a wishlist remove: async (event) => { - const { params, locals } = event; + const { locals } = event; const { user, session } = locals; if (userNotAuthenticated(user, session)) { return fail(401); @@ -129,7 +129,7 @@ export const actions: Actions = { if (game) { const wishlist = await db.query.wishlists.findFirst({ - where: eq(wishlists.user_id, locals.user.id), + where: eq(wishlists.user_id, user!.id!), }); if (!wishlist) { diff --git a/src/routes/(app)/(protected)/wishlists/[id]/+page.server.ts b/src/routes/(app)/(protected)/wishlists/[id]/+page.server.ts index 6852147..7d5f996 100644 --- a/src/routes/(app)/(protected)/wishlists/[id]/+page.server.ts +++ b/src/routes/(app)/(protected)/wishlists/[id]/+page.server.ts @@ -1,4 +1,4 @@ -import { error, type Actions } from '@sveltejs/kit'; +import { error, type Actions, fail } from '@sveltejs/kit'; import { and, eq } from 'drizzle-orm'; import { zod } from 'sveltekit-superforms/adapters'; import { superValidate } from 'sveltekit-superforms/server'; @@ -17,11 +17,9 @@ export async function load(event) { redirect(302, '/login', notSignedInMessage, event); } - console.log('Wishlist load User id', locals.user.id); - try { const wishlist = await db.query.wishlists.findMany({ - where: and(eq(wishlists.user_id, locals.user.id), eq(wishlists.cuid, id)), + where: and(eq(wishlists.user_id, user!.id!), eq(wishlists.cuid, id)), }); if (!wishlist) { @@ -66,7 +64,7 @@ export const actions: Actions = { if (game) { const wishlist = await db.query.wishlists.findFirst({ - where: eq(wishlists.user_id, locals.user.id), + where: eq(wishlists.user_id, user!.id!), }); if (!wishlist) { @@ -98,7 +96,7 @@ export const actions: Actions = { return error(405, 'Method not allowed'); }, // Delete a wishlist - delete: async ({ locals }) => { + delete: async (event) => { const { locals } = event; const { user, session } = locals; if (userNotAuthenticated(user, session)) { @@ -132,7 +130,7 @@ export const actions: Actions = { if (game) { const wishlist = await db.query.wishlists.findFirst({ - where: eq(wishlists.user_id, locals.user.id), + where: eq(wishlists.user_id, user!.id!), }); if (!wishlist) { diff --git a/src/routes/(app)/search/+page.server.ts b/src/routes/(app)/search/+page.server.ts index 8d98253..9701617 100644 --- a/src/routes/(app)/search/+page.server.ts +++ b/src/routes/(app)/search/+page.server.ts @@ -5,14 +5,13 @@ import kebabCase from 'just-kebab-case'; import type { GameType, SearchQuery } from '$lib/types'; import { mapAPIGameToBoredGame } from '$lib/utils/gameMapper.js'; import { search_schema } from '$lib/zodValidation'; -import type { PageServerLoad } from '../$types.js'; import type { BggThingDto } from 'boardgamegeekclient/dist/esm/dto/index.js'; import { createOrUpdateGameMinimal } from '$lib/utils/db/gameUtils'; async function searchForGames( locals: App.Locals, - eventFetch, - urlQueryParams: SearchQuery + eventFetch: typeof fetch, + urlQueryParams: URLSearchParams, ) { try { console.log('urlQueryParams search games', urlQueryParams); @@ -21,7 +20,7 @@ async function searchForGames( headers.set('Content-Type', 'application/json'); const requestInit: RequestInit = { method: 'GET', - headers + headers, }; const url = `/api/games/search${urlQueryParams ? `?${urlQueryParams}` : ''}`; console.log('Calling internal api', url); @@ -36,7 +35,7 @@ async function searchForGames( const games = await response.json(); console.log('games from DB', games); - const gameNameSearch = urlQueryParams.get('q'); + const gameNameSearch = urlQueryParams.get('q') ?? ''; let totalCount = games?.length || 0; if ( @@ -47,7 +46,7 @@ async function searchForGames( const searchQueryParams = urlQueryParams ? `?${urlQueryParams}` : ''; const externalResponse = await eventFetch( `/api/external/search${searchQueryParams}`, - requestInit + requestInit, ); console.log('Back from external search', externalResponse); @@ -65,10 +64,10 @@ async function searchForGames( console.log('totalCount', totalCount); for (const game of gameList) { console.log( - `Retrieving simplified external game details for id: ${game.id} with name ${game.name}` + `Retrieving simplified external game details for id: ${game.id} with name ${game.name}`, ); const externalGameResponse = await eventFetch( - `/api/external/game/${game.id}?simplified=true` + `/api/external/game/${game.id}?simplified=true`, ); if (externalGameResponse.ok) { const externalGame = await externalGameResponse.json(); @@ -82,14 +81,14 @@ async function searchForGames( return { totalCount, - games + games, }; } catch (e) { console.log(`Error searching board games ${e}`); } return { totalCount: 0, - games: [] + games: [], }; } @@ -108,18 +107,21 @@ export const load = async ({ locals, fetch, url }) => { searchParams.order = searchParams.order || defaults.order; searchParams.sort = searchParams.sort || defaults.sort; searchParams.q = searchParams.q || defaults.q; - const form = await superValidate({ - ...searchParams, - skip: Number(searchParams.skip || defaults.skip), - limit: Number(searchParams.limit || defaults.limit), - exact: searchParams.exact ? searchParams.exact === 'true' : defaults.exact - }, zod(search_schema)); + const form = await superValidate( + { + ...searchParams, + skip: Number(searchParams.skip || defaults.skip), + limit: Number(searchParams.limit || defaults.limit), + exact: searchParams.exact ? searchParams.exact === 'true' : defaults.exact, + }, + zod(search_schema), + ); const queryParams: SearchQuery = { limit: form.data?.limit, skip: form.data?.skip, q: form.data?.q, - exact: form.data?.exact + exact: form.data?.exact, }; try { @@ -129,8 +131,8 @@ export const load = async ({ locals, fetch, url }) => { searchData: { totalCount: 0, games: [], - wishlists: [] - } + wishlists: [], + }, }; } @@ -171,32 +173,32 @@ export const load = async ({ locals, fetch, url }) => { form, // modifyListForm, searchData, - wishlists: [] + wishlists: [], }; } catch (e) { console.log(`Error searching board games ${e}`); } - console.log('returning default no data') + console.log('returning default no data'); return { form, searchData: { totalCount: 0, - games: [] + games: [], }, - wishlists: [] + wishlists: [], }; }; export const actions = { - random: async ({ request, locals, fetch }): Promise => { + random: async ({ request, locals, fetch }) => { const form = await superValidate(request, zod(search_schema)); const queryParams: SearchQuery = { order_by: 'rank', ascending: false, random: true, fields: - 'id,name,min_age,min_players,max_players,thumb_url,min_playtime,max_playtime,min_age,description' + 'id,name,min_age,min_players,max_players,thumb_url,min_playtime,max_playtime,min_age,description', }; const newQueryParams: Record = {}; @@ -208,7 +210,7 @@ export const actions = { return { form, - searchData: await searchForGames(locals, fetch, urlQueryParams) + searchData: await searchForGames(locals, fetch, urlQueryParams), }; - } + }, }; diff --git a/src/routes/(app)/search/+page.svelte b/src/routes/(app)/search/+page.svelte index cbf0712..754df93 100644 --- a/src/routes/(app)/search/+page.svelte +++ b/src/routes/(app)/search/+page.svelte @@ -1,21 +1,28 @@ @@ -62,15 +61,6 @@ - {#if form?.twoFactorRequired} - - - Two Factor Code or Recovery Code - - - - - {/if} Login

By clicking continue, you agree to our @@ -86,20 +76,6 @@