mirror of
https://github.com/logto-io/logto.git
synced 2025-03-31 22:51:25 -05:00
fix(core): fix ac & ui proxy under subpath deployment (#1761)
fix ac & ui procy under subpath deployment
This commit is contained in:
parent
5447a7c5fe
commit
163c23b9bd
4 changed files with 23 additions and 7 deletions
|
@ -1,17 +1,22 @@
|
|||
import { MiddlewareType } from 'koa';
|
||||
import { IRouterParamContext } from 'koa-router';
|
||||
|
||||
import envSet from '@/env-set';
|
||||
import { appendPath } from '@/utils/url';
|
||||
|
||||
export default function koaRootProxy<
|
||||
StateT,
|
||||
ContextT extends IRouterParamContext,
|
||||
ResponseBodyT
|
||||
>(): MiddlewareType<StateT, ContextT, ResponseBodyT> {
|
||||
const { endpoint } = envSet.values;
|
||||
|
||||
return async (ctx, next) => {
|
||||
const requestPath = ctx.request.path;
|
||||
|
||||
// Redirect root path to the Admin Console welcome page
|
||||
if (requestPath === '/') {
|
||||
ctx.redirect('/welcome');
|
||||
ctx.redirect(appendPath(endpoint, '/welcome').toString());
|
||||
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -2,6 +2,9 @@ import { MiddlewareType } from 'koa';
|
|||
import { IRouterParamContext } from 'koa-router';
|
||||
import { Provider } from 'oidc-provider';
|
||||
|
||||
import envSet from '@/env-set';
|
||||
import { appendPath } from '@/utils/url';
|
||||
|
||||
// Need To Align With UI
|
||||
export const sessionNotFoundPath = '/unknown-session';
|
||||
export const guardedPath = ['/sign-in', '/register', '/social-register'];
|
||||
|
@ -11,6 +14,8 @@ export default function koaSpaSessionGuard<
|
|||
ContextT extends IRouterParamContext,
|
||||
ResponseBodyT
|
||||
>(provider: Provider): MiddlewareType<StateT, ContextT, ResponseBodyT> {
|
||||
const { endpoint } = envSet.values;
|
||||
|
||||
return async (ctx, next) => {
|
||||
const requestPath = ctx.request.path;
|
||||
const isPreview = ctx.request.URL.searchParams.get('preview');
|
||||
|
@ -20,7 +25,7 @@ export default function koaSpaSessionGuard<
|
|||
try {
|
||||
await provider.interactionDetails(ctx.req, ctx.res);
|
||||
} catch {
|
||||
ctx.redirect(sessionNotFoundPath);
|
||||
ctx.redirect(appendPath(endpoint, sessionNotFoundPath).toString());
|
||||
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { MountedApps } from '@/env-set';
|
||||
import envSet, { MountedApps } from '@/env-set';
|
||||
import { hasActiveUsers } from '@/queries/user';
|
||||
import { createContextWithRouteParameters } from '@/utils/test-utils';
|
||||
|
||||
|
@ -17,6 +17,7 @@ describe('koaWelcomeProxy', () => {
|
|||
});
|
||||
|
||||
it('should redirect to admin console if has AdminUsers', async () => {
|
||||
const { endpoint } = envSet.values;
|
||||
(hasActiveUsers as jest.Mock).mockResolvedValue(true);
|
||||
const ctx = createContextWithRouteParameters({
|
||||
url: `/${MountedApps.Welcome}`,
|
||||
|
@ -24,18 +25,19 @@ describe('koaWelcomeProxy', () => {
|
|||
|
||||
await koaWelcomeProxy()(ctx, next);
|
||||
|
||||
expect(ctx.redirect).toBeCalledWith(`/${MountedApps.Console}`);
|
||||
expect(ctx.redirect).toBeCalledWith(`${endpoint}/${MountedApps.Console}`);
|
||||
expect(next).not.toBeCalled();
|
||||
});
|
||||
|
||||
it('should redirect to welcome page if has no Users', async () => {
|
||||
const { endpoint } = envSet.values;
|
||||
(hasActiveUsers as jest.Mock).mockResolvedValue(false);
|
||||
const ctx = createContextWithRouteParameters({
|
||||
url: `/${MountedApps.Welcome}`,
|
||||
});
|
||||
|
||||
await koaWelcomeProxy()(ctx, next);
|
||||
expect(ctx.redirect).toBeCalledWith(`/${MountedApps.Console}/welcome`);
|
||||
expect(ctx.redirect).toBeCalledWith(`${endpoint}/${MountedApps.Console}/welcome`);
|
||||
expect(next).not.toBeCalled();
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,20 +1,24 @@
|
|||
import { MiddlewareType } from 'koa';
|
||||
import { IRouterParamContext } from 'koa-router';
|
||||
|
||||
import envSet from '@/env-set';
|
||||
import { hasActiveUsers } from '@/queries/user';
|
||||
import { appendPath } from '@/utils/url';
|
||||
|
||||
export default function koaWelcomeProxy<
|
||||
StateT,
|
||||
ContextT extends IRouterParamContext,
|
||||
ResponseBodyT
|
||||
>(): MiddlewareType<StateT, ContextT, ResponseBodyT> {
|
||||
const { adminConsoleUrl } = envSet.values;
|
||||
|
||||
return async (ctx) => {
|
||||
if (await hasActiveUsers()) {
|
||||
ctx.redirect('/console');
|
||||
ctx.redirect(adminConsoleUrl.toString());
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
ctx.redirect('/console/welcome');
|
||||
ctx.redirect(appendPath(adminConsoleUrl, '/welcome').toString());
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue