From 4496cd4b64e51805b790728e67a018791e731cd6 Mon Sep 17 00:00:00 2001 From: Dax Raad Date: Fri, 29 Aug 2025 11:58:17 -0400 Subject: ignore: cloud solid fixes --- cloud/app/src/context/auth.session.ts | 23 +++++ cloud/app/src/context/auth.ts | 78 +++++++++++++++++ cloud/app/src/context/auth.tsx | 105 ----------------------- cloud/app/src/context/auth.withActor.ts | 7 ++ cloud/app/src/routes/workspace/[workspaceID].tsx | 8 +- 5 files changed, 114 insertions(+), 107 deletions(-) create mode 100644 cloud/app/src/context/auth.session.ts create mode 100644 cloud/app/src/context/auth.ts delete mode 100644 cloud/app/src/context/auth.tsx create mode 100644 cloud/app/src/context/auth.withActor.ts (limited to 'cloud/app/src') diff --git a/cloud/app/src/context/auth.session.ts b/cloud/app/src/context/auth.session.ts new file mode 100644 index 000000000..c4b3940aa --- /dev/null +++ b/cloud/app/src/context/auth.session.ts @@ -0,0 +1,23 @@ +import { useSession } from "vinxi/http" + +export interface AuthSession { + account: Record< + string, + { + id: string + email: string + } + > + current?: string +} + +export function useAuthSession() { + return useSession({ + password: "0".repeat(32), + name: "auth", + cookie: { + secure: false, + httpOnly: true, + }, + }) +} diff --git a/cloud/app/src/context/auth.ts b/cloud/app/src/context/auth.ts new file mode 100644 index 000000000..9a5606a44 --- /dev/null +++ b/cloud/app/src/context/auth.ts @@ -0,0 +1,78 @@ +import { getRequestEvent } from "solid-js/web" +import { and, Database, eq, inArray } from "@opencode/cloud-core/drizzle/index.js" +import { WorkspaceTable } from "@opencode/cloud-core/schema/workspace.sql.js" +import { UserTable } from "@opencode/cloud-core/schema/user.sql.js" +import { query, redirect } from "@solidjs/router" +import { AccountTable } from "@opencode/cloud-core/schema/account.sql.js" +import { Actor } from "@opencode/cloud-core/actor.js" + +import { createClient } from "@openauthjs/openauth/client" +import { useAuthSession } from "./auth.session" + +export const AuthClient = createClient({ + clientID: "app", + issuer: import.meta.env.VITE_AUTH_URL, +}) + +export const getActor = query(async (): Promise => { + "use server" + const evt = getRequestEvent() + const url = new URL(evt!.request.headers.get("referer") ?? evt!.request.url) + const auth = await useAuthSession() + const splits = url.pathname.split("/").filter(Boolean) + if (splits[0] !== "workspace") { + if (auth.data.current) { + const current = auth.data.account[auth.data.current] + return { + type: "account", + properties: { + email: current.email, + accountID: current.id, + }, + } + } + if (Object.keys(auth.data.account ?? {}).length > 0) { + const current = Object.values(auth.data.account)[0] + await auth.update((val) => ({ + ...val, + current: current.id, + })) + return { + type: "account", + properties: { + email: current.email, + accountID: current.id, + }, + } + } + return { + type: "public", + properties: {}, + } + } + const workspaceHint = splits[1] + const accounts = Object.keys(auth.data.account) + const result = await Database.transaction(async (tx) => { + return await tx + .select({ + user: UserTable, + }) + .from(AccountTable) + .innerJoin(UserTable, and(eq(UserTable.email, AccountTable.email))) + .innerJoin(WorkspaceTable, eq(WorkspaceTable.id, UserTable.workspaceID)) + .where(and(inArray(AccountTable.id, accounts), eq(WorkspaceTable.id, workspaceHint))) + .limit(1) + .execute() + .then((x) => x[0]) + }) + if (result) { + return { + type: "user", + properties: { + userID: result.user.id, + workspaceID: result.user.workspaceID, + }, + } + } + throw redirect("/auth/authorize") +}, "actor") diff --git a/cloud/app/src/context/auth.tsx b/cloud/app/src/context/auth.tsx deleted file mode 100644 index 59fbff6e6..000000000 --- a/cloud/app/src/context/auth.tsx +++ /dev/null @@ -1,105 +0,0 @@ -import { useSession } from "vinxi/http" -import { createClient } from "@openauthjs/openauth/client" -import { getRequestEvent } from "solid-js/web" -import { and, Database, eq, inArray } from "@opencode/cloud-core/drizzle/index.js" -import { WorkspaceTable } from "@opencode/cloud-core/schema/workspace.sql.js" -import { UserTable } from "@opencode/cloud-core/schema/user.sql.js" -import { query, redirect } from "@solidjs/router" -import { AccountTable } from "@opencode/cloud-core/schema/account.sql.js" -import { Actor } from "@opencode/cloud-core/actor.js" - -export async function withActor(fn: () => T) { - const actor = await getActor() - return Actor.provide(actor.type, actor.properties, fn) -} - -export const getActor = query(async (): Promise => { - "use server" - const evt = getRequestEvent() - const url = new URL(evt!.request.headers.get("referer") ?? evt!.request.url) - const auth = await useAuthSession() - const [, workspaceHint] = url.pathname.split("/").filter((x) => x.length > 0) - if (!workspaceHint) { - if (auth.data.current) { - const current = auth.data.account[auth.data.current] - return { - type: "account", - properties: { - email: current.email, - accountID: current.id, - }, - } - } - if (Object.keys(auth.data.account ?? {}).length > 0) { - const current = Object.values(auth.data.account)[0] - await auth.update((val) => ({ - ...val, - current: current.id, - })) - return { - type: "account", - properties: { - email: current.email, - accountID: current.id, - }, - } - } - return { - type: "public", - properties: {}, - } - } - const accounts = Object.keys(auth.data.account) - const result = await Database.transaction(async (tx) => { - return await tx - .select({ - user: UserTable, - }) - .from(AccountTable) - .innerJoin(UserTable, and(eq(UserTable.email, AccountTable.email))) - .innerJoin(WorkspaceTable, eq(WorkspaceTable.id, UserTable.workspaceID)) - .where(and(inArray(AccountTable.id, accounts), eq(WorkspaceTable.id, workspaceHint))) - .limit(1) - .execute() - .then((x) => x[0]) - }) - if (result) { - return { - type: "user", - properties: { - userID: result.user.id, - workspaceID: result.user.workspaceID, - }, - } - } - throw redirect("/auth/authorize") -}, "actor") - -export const AuthClient = createClient({ - clientID: "app", - issuer: import.meta.env.VITE_AUTH_URL, -}) - -export interface AuthSession { - account: Record< - string, - { - id: string - email: string - } - > - current?: string -} - -export function useAuthSession() { - return useSession({ - password: "0".repeat(32), - name: "auth", - cookie: { - secure: false, - httpOnly: true, - }, - }) -} - -export function AuthProvider() { } diff --git a/cloud/app/src/context/auth.withActor.ts b/cloud/app/src/context/auth.withActor.ts new file mode 100644 index 000000000..a61b728ef --- /dev/null +++ b/cloud/app/src/context/auth.withActor.ts @@ -0,0 +1,7 @@ +import { Actor } from "@opencode/cloud-core/actor.js" +import { getActor } from "./auth" + +export async function withActor(fn: () => T) { + const actor = await getActor() + return Actor.provide(actor.type, actor.properties, fn) +} diff --git a/cloud/app/src/routes/workspace/[workspaceID].tsx b/cloud/app/src/routes/workspace/[workspaceID].tsx index 00e61a0c7..f507b5152 100644 --- a/cloud/app/src/routes/workspace/[workspaceID].tsx +++ b/cloud/app/src/routes/workspace/[workspaceID].tsx @@ -2,7 +2,8 @@ import { Billing } from "@opencode/cloud-core/billing.js" import { Key } from "@opencode/cloud-core/key.js" import { action, createAsync, revalidate, query, useAction, useSubmission } from "@solidjs/router" import { createEffect, createSignal, For, onMount, Show } from "solid-js" -import { getActor, withActor } from "~/context/auth" +import { getActor } from "~/context/auth" +import { withActor } from "~/context/auth.withActor" ///////////////////////////////////// // Keys related queries and actions @@ -47,8 +48,11 @@ const createPortalUrl = action(async (returnUrl: string) => { return withActor(() => Billing.generatePortalUrl({ returnUrl })) }, "portalUrl") -export default function() { +export default function () { const actor = createAsync(() => getActor()) + onMount(() => { + console.log("MOUNTED", actor()) + }) ///////////////// // Keys section -- cgit v1.2.3