Skip to content

Commit

Permalink
feat(core): set up proxy to host custom ui assets if available
Browse files Browse the repository at this point in the history
  • Loading branch information
charIeszhao committed Jul 16, 2024
1 parent ae4a127 commit 8ff16d2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
59 changes: 44 additions & 15 deletions packages/core/src/middleware/koa-spa-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,69 @@ import type { IRouterParamContext } from 'koa-router';

import { EnvSet } from '#src/env-set/index.js';
import serveStatic from '#src/middleware/koa-serve-static.js';
import type Queries from '#src/tenants/Queries.js';
import SystemContext from '#src/tenants/SystemContext.js';
import assertThat from '#src/utils/assert-that.js';

// eslint-disable-next-line max-params
export default function koaSpaProxy<StateT, ContextT extends IRouterParamContext, ResponseBodyT>(
mountedApps: string[],
packagePath = 'experience',
port = 5001,
prefix = ''
prefix = '',
queries?: Queries
): MiddlewareType<StateT, ContextT, ResponseBodyT> {
type Middleware = MiddlewareType<StateT, ContextT, ResponseBodyT>;

const distributionPath = path.join('node_modules/@logto', packagePath, 'dist');

const spaProxy: Middleware = EnvSet.values.isProduction
? serveStatic(distributionPath)
: proxy('*', {
target: `http://localhost:${port}`,
return async (ctx, next) => {
const requestPath = ctx.request.path;
const { signInExperiences } = queries ?? {};
const { customUiAssets } = (await signInExperiences?.findDefaultSignInExperience()) ?? {};

const spaProxy: Middleware = EnvSet.values.isProduction
? serveStatic(distributionPath)
: proxy('*', {
target: `http://localhost:${port}`,
changeOrigin: true,
logs: true,
rewrite: (requestPath) => {
// Static files
if (requestPath.includes('.')) {
return '/' + path.join(prefix, requestPath);
}

// In-app routes
return requestPath;
},
});

// Skip if the request is for another app
if (!prefix && mountedApps.some((app) => app !== prefix && requestPath.startsWith(`/${app}`))) {
return next();
}

// If user has uploaded custom UI assets, serve them instead of native experience UI
if (customUiAssets && packagePath === 'experience') {
const { rootUrl } = customUiAssets;
const { storageProviderConfig } = SystemContext.shared;
assertThat(storageProviderConfig, 'storage.not_configured');

const serveCustomUiAssets: Middleware = proxy('*', {
target: rootUrl,
changeOrigin: true,
logs: true,
rewrite: (requestPath) => {
// Static files
if (requestPath.includes('.')) {
return '/' + path.join(prefix, requestPath);
return '/' + requestPath;
}

// In-app routes
return requestPath;
return '/index.html';
},
});

return async (ctx, next) => {
const requestPath = ctx.request.path;

// Skip if the request is for another app
if (!prefix && mountedApps.some((app) => app !== prefix && requestPath.startsWith(`/${app}`))) {
return next();
return serveCustomUiAssets(ctx, next);
}

if (!EnvSet.values.isProduction) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/tenants/Tenant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export default class Tenant implements TenantContext {
koaExperienceSsr(libraries, queries),
koaSpaSessionGuard(provider, queries),
mount(`/${experience.routes.consent}`, koaAutoConsent(provider, queries)),
koaSpaProxy(mountedApps),
koaSpaProxy(mountedApps, undefined, undefined, undefined, queries),
])
);

Expand Down

0 comments on commit 8ff16d2

Please sign in to comment.