1+
2+
13import { useSession } from "vinxi/http"
24import { 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
485export const AuthClient = createClient ( {
586 clientID : "app" ,
6- issuer : "https://auth.dev.opencode.ai" ,
87+ issuer : import . meta . env . VITE_AUTH_URL ,
788} )
889
990export interface AuthSession {
@@ -15,7 +96,6 @@ export interface AuthSession {
1596}
1697
1798export function useAuthSession ( ) {
18- "use server"
1999
20100 return useSession < AuthSession > ( {
21101 password : "0" . repeat ( 32 ) ,
@@ -26,3 +106,4 @@ export function useAuthSession() {
26106
27107export function AuthProvider ( ) {
28108}
109+
0 commit comments