Skip to content

Commit

Permalink
fix(app): auth redirects not working because of upstream solid-start bug
Browse files Browse the repository at this point in the history
  • Loading branch information
vanillacode314 committed Sep 25, 2024
1 parent ba2e433 commit b5942aa
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
14 changes: 7 additions & 7 deletions apps/app/src/db/utils/boards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 './..';
Expand All @@ -14,7 +14,7 @@ async function $getBoards(path: string): Promise<Array<{ tasks: TTask[] } & TBoa
async function $getBoards(path: null | string) {
'use server';

const user = (await getUser({ redirectOnUnauthenticated: true }))!;
const user = await checkUser();

if (path === null) {
const $boards = await db.select().from(boards).where(eq(boards.userId, user.id));
Expand Down Expand Up @@ -49,7 +49,7 @@ const getBoards = cache($getBoards, 'get-boards');

const moveBoards = async (inputs: Array<{ id: TBoard['id']; index: number }>) => {
'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);
Expand Down Expand Up @@ -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 })
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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 =
Expand Down
12 changes: 6 additions & 6 deletions apps/app/src/db/utils/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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' });
Expand All @@ -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');
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();

Expand All @@ -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();
Expand Down
14 changes: 7 additions & 7 deletions apps/app/src/db/utils/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ 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 './..';

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;
Expand All @@ -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);
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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 =
Expand Down
8 changes: 8 additions & 0 deletions apps/app/src/utils/auth.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -254,6 +261,7 @@ async function decryptObjectKeys<T extends Record<string, unknown>>(
}

export {
checkUser,
decryptObjectKeys,
decryptWithUserKeys,
encryptWithUserKeys,
Expand Down

0 comments on commit b5942aa

Please sign in to comment.