diff --git a/apps/app/src/db/utils/boards.ts b/apps/app/src/db/utils/boards.ts index 0f0b794..99e3da7 100644 --- a/apps/app/src/db/utils/boards.ts +++ b/apps/app/src/db/utils/boards.ts @@ -3,7 +3,7 @@ import { and, asc, eq, gt, inArray, sql } from 'drizzle-orm'; import { nanoid } from 'nanoid'; import { boards, tasks, type TBoard, type TTask } from '~/db/schema'; -import { getUser } from '~/utils/auth.server'; +import { checkUser } from '~/utils/auth.server'; import { createNotifier } from '~/utils/publish'; import { db } from './..'; @@ -14,7 +14,7 @@ async function $getBoards(path: string): Promise) => { 'use server'; - const user = (await getUser())!; + const user = await checkUser(); if (inputs.length === 0) throw new Error('No boards to move'); const ids = inputs.map((input) => input.id); @@ -83,7 +83,7 @@ const moveBoards = async (inputs: Array<{ id: TBoard['id']; index: number }>) => const shiftBoard = async (boardId: TBoard['id'], direction: -1 | 1) => { 'use server'; - const user = (await getUser())!; + const user = await checkUser(); const [board] = await db .select({ index: boards.index }) @@ -118,7 +118,7 @@ const shiftBoard = async (boardId: TBoard['id'], direction: -1 | 1) => { const createBoard = action(async (formData: FormData) => { 'use server'; - const user = (await getUser())!; + const user = await checkUser(); const title = String(formData.get('title')).trim(); const id = String(formData.get('id') ?? nanoid()).trim(); @@ -152,7 +152,7 @@ const createBoard = action(async (formData: FormData) => { const updateBoard = action(async (formData: FormData) => { 'use server'; - const user = (await getUser())!; + const user = await checkUser(); const id = String(formData.get('id')).trim(); const title = String(formData.get('title')).trim(); @@ -172,7 +172,7 @@ const updateBoard = action(async (formData: FormData) => { const deleteBoard = action(async (formData: FormData) => { 'use server'; - const user = (await getUser())!; + const user = await checkUser(); const boardId = String(formData.get('id')).trim(); const publisherId = diff --git a/apps/app/src/db/utils/nodes.ts b/apps/app/src/db/utils/nodes.ts index 347ac74..36ec0d4 100644 --- a/apps/app/src/db/utils/nodes.ts +++ b/apps/app/src/db/utils/nodes.ts @@ -5,7 +5,7 @@ import { nanoid } from 'nanoid'; import { RESERVED_PATHS } from '~/consts'; import { boards, nodes, tasks, TBoard, TNode, TTask } from '~/db/schema'; import { uniqBy } from '~/utils/array'; -import { getUser } from '~/utils/auth.server'; +import { checkUser } from '~/utils/auth.server'; import * as path from '~/utils/path'; import { createNotifier } from '~/utils/publish'; @@ -15,7 +15,7 @@ const getNodes = cache( async (path: string, { includeChildren = false }: Partial<{ includeChildren: boolean }> = {}) => { 'use server'; - const user = (await getUser({ redirectOnUnauthenticated: true }))!; + const user = await checkUser(); const query = GET_NODES_BY_PATH_QUERY(path, user.id, { includeChildren, orderBy: 'name' }); const $nodes = (await db.all(sql.raw(query))) as TNode[]; if ($nodes.length === 0) return new Error(`Not Found`, { cause: 'NOT_FOUND' }); @@ -28,7 +28,7 @@ const getNodes = cache( const createNode = action(async (formData: FormData) => { 'use server'; - const user = (await getUser())!; + const user = await checkUser(); const name = String(formData.get('name')).trim(); if (!name) throw new Error('name is required'); @@ -68,7 +68,7 @@ const createNode = action(async (formData: FormData) => { const updateNode = action(async (formData: FormData) => { 'use server'; - const user = (await getUser())!; + const user = await checkUser(); const id = String(formData.get('id')).trim(); const name = String(formData.get('name')).trim(); @@ -98,7 +98,7 @@ const updateNode = action(async (formData: FormData) => { const deleteNode = action(async (formData: FormData) => { 'use server'; - const user = (await getUser())!; + const user = await checkUser(); const nodeId = String(formData.get('id')).trim(); @@ -113,7 +113,7 @@ const deleteNode = action(async (formData: FormData) => { async function $copyNode(formData: FormData) { 'use server'; - const user = (await getUser())!; + const user = await checkUser(); const id = String(formData.get('id')).trim(); const parentId = String(formData.get('parentId')).trim(); diff --git a/apps/app/src/db/utils/tasks.ts b/apps/app/src/db/utils/tasks.ts index 27e7f65..595ca1a 100644 --- a/apps/app/src/db/utils/tasks.ts +++ b/apps/app/src/db/utils/tasks.ts @@ -3,7 +3,7 @@ import { and, eq, gt, inArray, sql } from 'drizzle-orm'; import { nanoid } from 'nanoid'; import { tasks, type TBoard, type TTask } from '~/db/schema'; -import { getUser } from '~/utils/auth.server'; +import { checkUser } from '~/utils/auth.server'; import { createNotifier } from '~/utils/publish'; import { db } from './..'; @@ -11,7 +11,7 @@ import { db } from './..'; const getTasks = cache(async function () { 'use server'; - const user = (await getUser({ redirectOnUnauthenticated: true }))!; + const user = await checkUser(); const $tasks = await db.select().from(tasks).where(eq(tasks.userId, user.id)); return $tasks; @@ -21,7 +21,7 @@ const moveTasks = async ( inputs: Array<{ boardId: TBoard['id']; id: TTask['id']; index: number }> ) => { 'use server'; - const user = (await getUser())!; + const user = await checkUser(); if (inputs.length === 0) throw new Error('No tasks to move'); const ids = inputs.map((input) => input.id); @@ -62,7 +62,7 @@ const moveTasks = async ( const shiftTask = async (taskId: TTask['id'], direction: -1 | 1) => { 'use server'; - const user = (await getUser())!; + const user = await checkUser(); const [task] = await db .select() @@ -99,7 +99,7 @@ const shiftTask = async (taskId: TTask['id'], direction: -1 | 1) => { const createTask = action(async (formData: FormData) => { 'use server'; - const user = (await getUser())!; + const user = await checkUser(); const title = String(formData.get('title')).trim(); const boardId = String(formData.get('boardId')).trim(); @@ -128,7 +128,7 @@ const createTask = action(async (formData: FormData) => { const updateTask = action(async (formData: FormData) => { 'use server'; - const user = (await getUser())!; + const user = await checkUser(); const id = String(formData.get('id')).trim(); const title = String(formData.get('title')).trim(); @@ -147,7 +147,7 @@ const updateTask = action(async (formData: FormData) => { const deleteTask = action(async (formData: FormData) => { 'use server'; - const user = (await getUser())!; + const user = await checkUser(); const taskId = String(formData.get('id')).trim(); const publisherId = diff --git a/apps/app/src/utils/auth.server.ts b/apps/app/src/utils/auth.server.ts index df4dd17..173d777 100644 --- a/apps/app/src/utils/auth.server.ts +++ b/apps/app/src/utils/auth.server.ts @@ -15,6 +15,13 @@ import env from './env/server'; import { localforage } from './localforage'; import { resend } from './resend.server'; +// NOTE: only need till the upstream solid-start bug is fixed https://github.com/solidjs/solid-start/issues/1624 +async function checkUser() { + const user = await getUser({ shouldThrow: false }); + if (!user) throw redirect('/auth/signin'); + return user; +} + const getUser = cache( async ({ redirectOnAuthenticated = false, @@ -254,6 +261,7 @@ async function decryptObjectKeys>( } export { + checkUser, decryptObjectKeys, decryptWithUserKeys, encryptWithUserKeys,