diff options
| author | Dax Raad <[email protected]> | 2025-08-20 16:52:43 -0400 |
|---|---|---|
| committer | Dax Raad <[email protected]> | 2025-08-20 17:01:18 -0400 |
| commit | 522bed6b7dabd09328b3c8aae90b06ab06344623 (patch) | |
| tree | 905dca3e2b3fb4e765d03f2ec78e285f19a31155 /cloud/app/src/context | |
| parent | dda672284c69ad399029316a63677cf49cbc54f3 (diff) | |
| download | opencode-522bed6b7dabd09328b3c8aae90b06ab06344623.tar.gz opencode-522bed6b7dabd09328b3c8aae90b06ab06344623.zip | |
ignore: cloud stuff
Diffstat (limited to 'cloud/app/src/context')
| -rw-r--r-- | cloud/app/src/context/auth.tsx | 85 |
1 files changed, 83 insertions, 2 deletions
diff --git a/cloud/app/src/context/auth.tsx b/cloud/app/src/context/auth.tsx index bec949568..88d3214c1 100644 --- a/cloud/app/src/context/auth.tsx +++ b/cloud/app/src/context/auth.tsx @@ -1,9 +1,90 @@ + + 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<T>(fn: () => T) { + const actor = await getActor() + return Actor.provide(actor.type, actor.properties, fn) +} + +export const getActor = query(async (): Promise<Actor.Info> => { + "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: "https://auth.dev.opencode.ai", + issuer: import.meta.env.VITE_AUTH_URL, }) export interface AuthSession { @@ -15,7 +96,6 @@ export interface AuthSession { } export function useAuthSession() { - "use server" return useSession<AuthSession>({ password: "0".repeat(32), @@ -26,3 +106,4 @@ export function useAuthSession() { export function AuthProvider() { } + |
