mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-11-07 20:44:05 +09:00
## Description Implements entri service support ## Steps for reproduction Wait for SaaS release. Or locally add following env variables to the `.env` file ``` ENTRI_APPLICATION_ID=webstudio ENTRI_SECRET=$(bwget WS_ENTRI_SECRET) ``` <img width="361" alt="image" src="https://github.com/webstudio-is/webstudio-builder/assets/5077042/eafcc7ec-1bc7-448f-be78-50ae89909d34"> <img width="724" alt="image" src="https://github.com/webstudio-is/webstudio-builder/assets/5077042/6d97a08b-1763-4ca4-970f-96836b4d5cd2"> ## Code Review - [ ] hi @kof, I need you to do - conceptual review (architecture, feature-correctness) - detailed review (read every line) - test it on preview ## Before requesting a review - [ ] made a self-review - [ ] added inline comments where things may be not obvious (the "why", not "what") ## Before merging - [ ] tested locally and on preview environment (preview dev login: 5de6) - [ ] updated [test cases](https://github.com/webstudio-is/webstudio-builder/blob/main/apps/builder/docs/test-cases.md) document - [ ] added tests - [ ] if any new env variables are added, added them to `.env.example` and the `builder/env-check.js` if mandatory --------- Co-authored-by: Oleg Isonen <oleg008@gmail.com>
73 lines
1.8 KiB
TypeScript
73 lines
1.8 KiB
TypeScript
import type { AppContext } from "@webstudio-is/trpc-interface/index.server";
|
|
import env from "~/env/env.server";
|
|
import { authenticator } from "~/services/auth.server";
|
|
import { trpcClient } from "~/services/trpc.server";
|
|
import { entryApi } from "./entri/entri-api.server";
|
|
|
|
const createAuthorizationContext = async (
|
|
request: Request
|
|
): Promise<AppContext["authorization"]> => {
|
|
const url = new URL(request.url);
|
|
|
|
const authToken = url.searchParams.get("authToken") ?? url.hostname;
|
|
|
|
const user = await authenticator.isAuthenticated(request);
|
|
|
|
const isServiceCall =
|
|
request.headers.has("Authorization") &&
|
|
request.headers.get("Authorization") === env.TRPC_SERVER_API_TOKEN;
|
|
|
|
const context: AppContext["authorization"] = {
|
|
userId: user?.id,
|
|
authToken,
|
|
isServiceCall,
|
|
authorizeTrpc: trpcClient.authorize,
|
|
};
|
|
|
|
return context;
|
|
};
|
|
|
|
const createDomainContext = (request: Request) => {
|
|
const context: AppContext["domain"] = {
|
|
domainTrpc: trpcClient.domain,
|
|
};
|
|
|
|
return context;
|
|
};
|
|
|
|
const createDeploymentContext = (request: Request) => {
|
|
const url = new URL(request.url);
|
|
|
|
const context: AppContext["deployment"] = {
|
|
deploymentTrpc: trpcClient.deployment,
|
|
env: {
|
|
BUILDER_ORIGIN: url.origin,
|
|
BRANCH_NAME: env.BRANCH_NAME ?? "main",
|
|
},
|
|
};
|
|
|
|
return context;
|
|
};
|
|
|
|
const createEntriContext = () => {
|
|
return {
|
|
entryApi,
|
|
};
|
|
};
|
|
|
|
/**
|
|
* argument buildEnv==="prod" only if we are loading project with production build
|
|
*/
|
|
export const createContext = async (request: Request): Promise<AppContext> => {
|
|
const authorization = await createAuthorizationContext(request);
|
|
const domain = createDomainContext(request);
|
|
const deployment = createDeploymentContext(request);
|
|
const entri = createEntriContext();
|
|
return {
|
|
authorization,
|
|
domain,
|
|
deployment,
|
|
entri,
|
|
};
|
|
};
|