summaryrefslogtreecommitdiffhomepage
path: root/packages/cloud/app/src/context/auth.ts
diff options
context:
space:
mode:
authorFrank <[email protected]>2025-09-18 01:32:40 -0400
committerFrank <[email protected]>2025-09-18 01:32:40 -0400
commitfc4f281408c56ab12db571a470456212a479edf5 (patch)
tree309d23b0c497bc61af6f8e650a6036fa41d7cbdb /packages/cloud/app/src/context/auth.ts
parentf8c4f713a5b48892899d0ac195c3470ab7ef764c (diff)
downloadopencode-fc4f281408c56ab12db571a470456212a479edf5.tar.gz
opencode-fc4f281408c56ab12db571a470456212a479edf5.zip
wip: zen
Diffstat (limited to 'packages/cloud/app/src/context/auth.ts')
-rw-r--r--packages/cloud/app/src/context/auth.ts83
1 files changed, 83 insertions, 0 deletions
diff --git a/packages/cloud/app/src/context/auth.ts b/packages/cloud/app/src/context/auth.ts
new file mode 100644
index 000000000..e08d965b8
--- /dev/null
+++ b/packages/cloud/app/src/context/auth.ts
@@ -0,0 +1,83 @@
+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 { 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 = 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
+}