@@ -7,7 +7,6 @@ import { entryApi } from "./entri/entri-api.server";
77import { getUserPlanFeatures } from "./db/user-plan-features.server" ;
88import { staticEnv } from "~/env/env.static.server" ;
99import { createClient } from "@webstudio-is/postrest/index.server" ;
10- import { prisma } from "@webstudio-is/prisma-client" ;
1110import { builderAuthenticator } from "~/services/builder-auth.server" ;
1211import { readLoginSessionBloomFilter } from "~/services/session.server" ;
1312import type { BloomFilter } from "~/services/bloom-filter.server" ;
@@ -40,29 +39,24 @@ export const extractAuthFromRequest = async (request: Request) => {
4039 } ;
4140} ;
4241
43- const createTokenAuthorizationContext = async ( authToken : string ) => {
44- const projectOwnerIdByToken = await prisma . authorizationToken . findUnique ( {
45- where : {
46- token : authToken ,
47- } ,
48- select : {
49- project : {
50- select : {
51- id : true ,
52- userId : true ,
53- } ,
54- } ,
55- } ,
56- } ) ;
42+ const createTokenAuthorizationContext = async (
43+ authToken : string ,
44+ postgrest : AppContext [ "postgrest" ]
45+ ) => {
46+ const projectOwnerIdByToken = await postgrest . client
47+ . from ( "AuthorizationToken" )
48+ . select ( "project:Project(id, userId)" )
49+ . eq ( "token" , authToken )
50+ . single ( ) ;
5751
58- if ( projectOwnerIdByToken === null ) {
52+ if ( projectOwnerIdByToken . error ) {
5953 throw new Error ( `Project owner can't be found for token ${ authToken } ` ) ;
6054 }
6155
62- const ownerId = projectOwnerIdByToken . project . userId ;
56+ const ownerId = projectOwnerIdByToken . data . project ? .userId ?? null ;
6357 if ( ownerId === null ) {
6458 throw new Error (
65- `Project ${ projectOwnerIdByToken . project . id } has null userId`
59+ `Project ${ projectOwnerIdByToken . data . project ? .id } has null userId`
6660 ) ;
6761 }
6862
@@ -74,7 +68,8 @@ const createTokenAuthorizationContext = async (authToken: string) => {
7468} ;
7569
7670const createAuthorizationContext = async (
77- request : Request
71+ request : Request ,
72+ postgrest : AppContext [ "postgrest" ]
7873) : Promise < AppContext [ "authorization" ] > => {
7974 const { authToken, isServiceCall, sessionData } =
8075 await extractAuthFromRequest ( request ) ;
@@ -87,7 +82,7 @@ const createAuthorizationContext = async (
8782 }
8883
8984 if ( authToken != null ) {
90- return await createTokenAuthorizationContext ( authToken ) ;
85+ return await createTokenAuthorizationContext ( authToken , postgrest ) ;
9186 }
9287
9388 if ( sessionData ?. userId != null ) {
@@ -155,7 +150,8 @@ const createEntriContext = () => {
155150} ;
156151
157152const createUserPlanContext = async (
158- authorization : AppContext [ "authorization" ]
153+ authorization : AppContext [ "authorization" ] ,
154+ postgrest : AppContext [ "postgrest" ]
159155) => {
160156 const ownerId =
161157 authorization . type === "token"
@@ -164,7 +160,9 @@ const createUserPlanContext = async (
164160 ? authorization . userId
165161 : undefined ;
166162
167- const planFeatures = ownerId ? await getUserPlanFeatures ( ownerId ) : undefined ;
163+ const planFeatures = ownerId
164+ ? await getUserPlanFeatures ( ownerId , postgrest )
165+ : undefined ;
168166 return planFeatures ;
169167} ;
170168
@@ -193,18 +191,27 @@ export const createPostrestContext = () => {
193191 * argument buildEnv==="prod" only if we are loading project with production build
194192 */
195193export const createContext = async ( request : Request ) : Promise < AppContext > => {
196- const authorization = await createAuthorizationContext ( request ) ;
194+ const postgrest = createPostrestContext ( ) ;
195+ const authorization = await createAuthorizationContext ( request , postgrest ) ;
197196
198197 const domain = createDomainContext ( ) ;
199198 const deployment = createDeploymentContext ( getRequestOrigin ( request . url ) ) ;
200199 const entri = createEntriContext ( ) ;
201- const userPlanFeatures = await createUserPlanContext ( authorization ) ;
200+ const userPlanFeatures = await createUserPlanContext (
201+ authorization ,
202+ postgrest
203+ ) ;
202204 const trpcCache = createTrpcCache ( ) ;
203- const postgrest = createPostrestContext ( ) ;
204205
205206 const createTokenContext = async ( authToken : string ) => {
206- const authorization = await createTokenAuthorizationContext ( authToken ) ;
207- const userPlanFeatures = await createUserPlanContext ( authorization ) ;
207+ const authorization = await createTokenAuthorizationContext (
208+ authToken ,
209+ postgrest
210+ ) ;
211+ const userPlanFeatures = await createUserPlanContext (
212+ authorization ,
213+ postgrest
214+ ) ;
208215
209216 return {
210217 authorization,
0 commit comments