Skip to content

Commit 522bed6

Browse files
committed
ignore: cloud stuff
1 parent dda6722 commit 522bed6

18 files changed

Lines changed: 180 additions & 30 deletions

File tree

bun.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cloud/app/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"@solidjs/router": "^0.15.0",
1515
"@solidjs/start": "^1.1.0",
1616
"solid-js": "^1.9.5",
17-
"vinxi": "^0.5.7"
17+
"vinxi": "^0.5.7",
18+
"@opencode/cloud-core": "workspace:*"
1819
},
1920
"engines": {
2021
"node": ">=22"

cloud/app/src/app.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { MetaProvider, Title } from "@solidjs/meta";
22
import { Router } from "@solidjs/router";
33
import { FileRoutes } from "@solidjs/start/router";
4-
import { Suspense } from "solid-js";
4+
import { ErrorBoundary, Suspense } from "solid-js";
55
import "@ibm/plex/css/ibm-plex.css";
66
import "./app.css";
77

@@ -11,7 +11,9 @@ export default function App() {
1111
root={props => (
1212
<MetaProvider>
1313
<Title>SolidStart - Basic</Title>
14-
<Suspense>{props.children}</Suspense>
14+
<ErrorBoundary fallback={<div>Something went wrong</div>}>
15+
<Suspense>{props.children}</Suspense>
16+
</ErrorBoundary>
1517
</MetaProvider>
1618
)}
1719
>

cloud/app/src/context/auth.tsx

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,90 @@
1+
2+
13
import { useSession } from "vinxi/http"
24
import { createClient } from "@openauthjs/openauth/client"
5+
import { getRequestEvent } from "solid-js/web"
6+
import { and, Database, eq, inArray } from "@opencode/cloud-core/drizzle/index.js"
7+
import { WorkspaceTable } from "@opencode/cloud-core/schema/workspace.sql.js"
8+
import { UserTable } from "@opencode/cloud-core/schema/user.sql.js"
9+
import { query, redirect } from "@solidjs/router"
10+
import { AccountTable } from "@opencode/cloud-core/schema/account.sql.js"
11+
import { Actor } from "@opencode/cloud-core/actor.js"
12+
13+
export async function withActor<T>(fn: () => T) {
14+
const actor = await getActor()
15+
return Actor.provide(actor.type, actor.properties, fn)
16+
}
17+
18+
export const getActor = query(async (): Promise<Actor.Info> => {
19+
"use server"
20+
const evt = getRequestEvent()
21+
const url = new URL(evt!.request.headers.get("referer") ?? evt!.request.url)
22+
const auth = await useAuthSession()
23+
const [workspaceHint] = url.pathname.split("/").filter((x) => x.length > 0)
24+
if (!workspaceHint) {
25+
if (auth.data.current) {
26+
const current = auth.data.account[auth.data.current]
27+
return {
28+
type: "account",
29+
properties: {
30+
email: current.email,
31+
accountID: current.id,
32+
},
33+
}
34+
}
35+
if (Object.keys(auth.data.account).length > 0) {
36+
const current = Object.values(auth.data.account)[0]
37+
await auth.update(val => ({
38+
...val,
39+
current: current.id,
40+
}))
41+
return {
42+
type: "account",
43+
properties: {
44+
email: current.email,
45+
accountID: current.id,
46+
},
47+
}
48+
}
49+
return {
50+
type: "public",
51+
properties: {},
52+
}
53+
}
54+
const accounts = Object.keys(auth.data.account)
55+
const result = await Database.transaction(async (tx) => {
56+
return await tx.select({
57+
user: UserTable
58+
})
59+
.from(AccountTable)
60+
.innerJoin(UserTable, and(eq(UserTable.email, AccountTable.email)))
61+
.innerJoin(WorkspaceTable, eq(WorkspaceTable.id, UserTable.workspaceID))
62+
.where(
63+
and(
64+
inArray(AccountTable.id, accounts),
65+
eq(WorkspaceTable.id, workspaceHint),
66+
)
67+
)
68+
.limit(1)
69+
.execute()
70+
.then((x) => x[0])
71+
})
72+
if (result) {
73+
return {
74+
type: "user",
75+
properties: {
76+
userID: result.user.id,
77+
workspaceID: result.user.workspaceID,
78+
},
79+
}
80+
}
81+
throw redirect("/auth/authorize")
82+
}, "actor")
83+
384

485
export const AuthClient = createClient({
586
clientID: "app",
6-
issuer: "https://auth.dev.opencode.ai",
87+
issuer: import.meta.env.VITE_AUTH_URL,
788
})
889

990
export interface AuthSession {
@@ -15,7 +96,6 @@ export interface AuthSession {
1596
}
1697

1798
export function useAuthSession() {
18-
"use server"
1999

20100
return useSession<AuthSession>({
21101
password: "0".repeat(32),
@@ -26,3 +106,4 @@ export function useAuthSession() {
26106

27107
export function AuthProvider() {
28108
}
109+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { createAsync, query } from "@solidjs/router"
2+
import { getActor, withActor } from "~/context/auth"
3+
4+
const getPosts = query(async () => {
5+
"use server"
6+
return withActor(() => {
7+
return "ok"
8+
})
9+
}, "posts")
10+
11+
12+
export default function () {
13+
const actor = createAsync(async () => getActor())
14+
return <div>{JSON.stringify(actor())}</div>
15+
}

cloud/app/src/routes/auth/callback.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ export async function GET(input: APIEvent) {
55
const url = new URL(input.request.url)
66
const code = url.searchParams.get("code")
77
if (!code) throw new Error("No code found")
8-
const redirectURI = `${url.origin}${url.pathname}`
9-
console.log({
10-
redirectURI,
11-
code,
12-
})
138
const result = await AuthClient.exchange(code, `${url.origin}${url.pathname}`)
149
if (result.err) {
1510
throw new Error(result.err.message)

cloud/app/src/routes/index.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import IMG_SPLASH from "../asset/screenshot-splash.webp"
66
import IMG_VSCODE from "../asset/screenshot-vscode.webp"
77
import IMG_GITHUB from "../asset/screenshot-github.webp"
88
import { IconCopy, IconCheck } from "../component/icon"
9+
import { createAsync, query, redirect, RouteDefinition } from "@solidjs/router"
10+
import { getActor, withActor } from "~/context/auth"
11+
import { Account } from "@opencode/cloud-core/account.js"
912

1013
function CopyStatus() {
1114
return (
@@ -16,7 +19,22 @@ function CopyStatus() {
1619
)
1720
}
1821

22+
const isLoggedIn = query(async () => {
23+
"use server"
24+
const actor = await getActor()
25+
if (actor.type === "account") {
26+
const workspaces = await withActor(() => Account.workspaces())
27+
throw redirect("/" + workspaces[0].id)
28+
}
29+
return
30+
}, "isLoggedIn")
31+
32+
33+
1934
export default function Home() {
35+
createAsync(() => isLoggedIn(), {
36+
deferStream: true,
37+
})
2038
onMount(() => {
2139
const commands = document.querySelectorAll("[data-copy]")
2240
for (const button of commands) {

cloud/app/sst-env.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* This file is auto-generated by SST. Do not edit. */
2+
/* tslint:disable */
3+
/* eslint-disable */
4+
/* deno-fmt-ignore-file */
5+
6+
/// <reference path="../../sst-env.d.ts" />
7+
8+
import "sst"
9+
export {}

cloud/core/src/actor.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export namespace Actor {
2020
properties: {
2121
userID: string
2222
workspaceID: string
23-
email: string
2423
}
2524
}
2625

cloud/core/src/drizzle/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Resource } from "sst"
33
export * from "drizzle-orm"
44
import postgres from "postgres"
55

6-
function createClient() {
6+
const createClient = memo(() => {
77
const client = postgres({
88
idle_timeout: 30000,
99
connect_timeout: 30000,
@@ -19,12 +19,13 @@ function createClient() {
1919
})
2020

2121
return drizzle(client, {})
22-
}
22+
})
2323

2424
import { PgTransaction, type PgTransactionConfig } from "drizzle-orm/pg-core"
2525
import type { ExtractTablesWithRelations } from "drizzle-orm"
2626
import type { PostgresJsQueryResultHKT } from "drizzle-orm/postgres-js"
2727
import { Context } from "../context"
28+
import { memo } from "../util/memo"
2829

2930
export namespace Database {
3031
export type Transaction = PgTransaction<

0 commit comments

Comments
 (0)