summaryrefslogtreecommitdiffhomepage
path: root/packages/console/app/src/context
diff options
context:
space:
mode:
authorFrank <[email protected]>2025-09-18 10:59:01 -0400
committerFrank <[email protected]>2025-09-18 10:59:01 -0400
commit4ceabdffa07b1af8d99eb73622a4d549d99ec6d2 (patch)
tree72e2ae62084a9e24cc76caffbd1f30dafc69ea56 /packages/console/app/src/context
parentc87480cf931a6f8f8b55552558ef521f1918b578 (diff)
downloadopencode-4ceabdffa07b1af8d99eb73622a4d549d99ec6d2.tar.gz
opencode-4ceabdffa07b1af8d99eb73622a4d549d99ec6d2.zip
wip: zen
Diffstat (limited to 'packages/console/app/src/context')
-rw-r--r--packages/console/app/src/context/auth.session.ts23
-rw-r--r--packages/console/app/src/context/auth.ts83
-rw-r--r--packages/console/app/src/context/auth.withActor.ts7
3 files changed, 113 insertions, 0 deletions
diff --git a/packages/console/app/src/context/auth.session.ts b/packages/console/app/src/context/auth.session.ts
new file mode 100644
index 000000000..609bc364b
--- /dev/null
+++ b/packages/console/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<AuthSession>({
+ password: "0".repeat(32),
+ name: "auth",
+ cookie: {
+ secure: false,
+ httpOnly: true,
+ },
+ })
+}
diff --git a/packages/console/app/src/context/auth.ts b/packages/console/app/src/context/auth.ts
new file mode 100644
index 000000000..027885241
--- /dev/null
+++ b/packages/console/app/src/context/auth.ts
@@ -0,0 +1,83 @@
+import { getRequestEvent } from "solid-js/web"
+import { and, Database, eq, inArray } from "@opencode/console-core/drizzle/index.js"
+import { WorkspaceTable } from "@opencode/console-core/schema/workspace.sql.js"
+import { UserTable } from "@opencode/console-core/schema/user.sql.js"
+import { redirect } from "@solidjs/router"
+import { AccountTable } from "@opencode/console-core/schema/account.sql.js"
+import { Actor } from "@opencode/console-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 = async (workspace?: string): Promise<Actor.Info> => {
+ "use server"
+ const evt = getRequestEvent()
+ if (!evt) throw new Error("No request event")
+ if (evt.locals.actor) return evt.locals.actor
+ evt.locals.actor = (async () => {
+ const auth = await useAuthSession()
+ if (!workspace) {
+ const account = auth.data.account ?? {}
+ const current = account[auth.data.current ?? ""]
+ if (current) {
+ return {
+ type: "account",
+ properties: {
+ email: current.email,
+ accountID: current.id,
+ },
+ }
+ }
+ if (Object.keys(account).length > 0) {
+ const current = Object.values(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 ?? {})
+ if (accounts.length) {
+ 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, workspace)))
+ .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")
+ })()
+ return evt.locals.actor
+}
diff --git a/packages/console/app/src/context/auth.withActor.ts b/packages/console/app/src/context/auth.withActor.ts
new file mode 100644
index 000000000..2cb970269
--- /dev/null
+++ b/packages/console/app/src/context/auth.withActor.ts
@@ -0,0 +1,7 @@
+import { Actor } from "@opencode/console-core/actor.js"
+import { getActor } from "./auth"
+
+export async function withActor<T>(fn: () => T, workspace?: string) {
+ const actor = await getActor(workspace)
+ return Actor.provide(actor.type, actor.properties, fn)
+}