0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-02-03 21:48:55 -05:00

feat(core): empty path sould redirect to the console page (#915)

* feat(core): empty path sould redirect to the console page

empty path should redirect to the console page

* fix(core): remove console log

remove console log

* fix(core): cr fix

cr fix
This commit is contained in:
simeng-li 2022-05-22 10:32:04 +08:00 committed by GitHub
parent 5d7f2e436c
commit 207c404aeb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 12 deletions

View file

@ -7,12 +7,12 @@ import koaLogger from 'koa-logger';
import mount from 'koa-mount';
import envSet, { MountedApps } from '@/env-set';
import koaClientSessionGuard from '@/middleware/koa-client-session-guard';
import koaConnectorErrorHandler from '@/middleware/koa-connector-error-handle';
import koaErrorHandler from '@/middleware/koa-error-handler';
import koaI18next from '@/middleware/koa-i18next';
import koaLog from '@/middleware/koa-log';
import koaOIDCErrorHandler from '@/middleware/koa-oidc-error-handler';
import koaProxyGuard from '@/middleware/koa-proxy-guard';
import koaSlonikErrorHandler from '@/middleware/koa-slonik-error-handler';
import koaSpaProxy from '@/middleware/koa-spa-proxy';
import initOidc from '@/oidc/init';
@ -39,7 +39,7 @@ export default async function initApp(app: Koa): Promise<void> {
mount('/' + MountedApps.Console, koaSpaProxy(MountedApps.Console, 5002, MountedApps.Console))
);
app.use(koaClientSessionGuard(provider));
app.use(koaProxyGuard(provider));
app.use(koaSpaProxy());
const { httpsCert, httpsKey, port } = envSet.values;

View file

@ -3,7 +3,7 @@ import { Provider } from 'oidc-provider';
import { MountedApps } from '@/env-set';
import { createContextWithRouteParameters } from '@/utils/test-utils';
import koaClientSessionGuard, { sessionNotFoundPath } from './koa-client-session-guard';
import koaProxyGuard, { sessionNotFoundPath } from './koa-proxy-guard';
jest.mock('fs/promises', () => ({
...jest.requireActual('fs/promises'),
@ -16,7 +16,7 @@ jest.mock('oidc-provider', () => ({
})),
}));
describe('koaClientSessionGuard', () => {
describe('koaProxyGuard', () => {
const envBackup = process.env;
beforeEach(() => {
@ -38,7 +38,7 @@ describe('koaClientSessionGuard', () => {
url: `/${app}/foo`,
});
await koaClientSessionGuard(provider)(ctx, next);
await koaProxyGuard(provider)(ctx, next);
expect(ctx.redirect).not.toBeCalled();
});
@ -49,7 +49,7 @@ describe('koaClientSessionGuard', () => {
const ctx = createContextWithRouteParameters({
url: `/sign-in`,
});
await koaClientSessionGuard(provider)(ctx, next);
await koaProxyGuard(provider)(ctx, next);
expect(ctx.redirect).not.toBeCalled();
});
@ -60,7 +60,7 @@ describe('koaClientSessionGuard', () => {
const ctx = createContextWithRouteParameters({
url: `${sessionNotFoundPath}`,
});
await koaClientSessionGuard(provider)(ctx, next);
await koaProxyGuard(provider)(ctx, next);
expect(ctx.redirect).not.toBeCalled();
});
@ -71,7 +71,7 @@ describe('koaClientSessionGuard', () => {
const ctx = createContextWithRouteParameters({
url: '/sign-in',
});
await koaClientSessionGuard(provider)(ctx, next);
await koaProxyGuard(provider)(ctx, next);
expect(ctx.redirect).toBeCalled();
});
});

View file

@ -18,18 +18,25 @@ export default function koaSpaSessionGuard<
return async (ctx, next) => {
const requestPath = ctx.request.path;
const packagesPath = fromRoot ? 'packages/' : '..';
const distPath = path.join(packagesPath, 'ui', 'dist');
const clientPath = path.join(packagesPath, 'ui', 'dist');
// Guard client routes only
// Empty path Redirect
if (requestPath === '/') {
ctx.redirect(`/${MountedApps.Console}`);
return next();
}
// Check client routes session status only
if (Object.values(MountedApps).some((app) => requestPath.startsWith(`/${app}`))) {
return next();
}
// Client session guard
try {
// Find session
await provider.interactionDetails(ctx.req, ctx.res);
} catch {
const spaDistFiles = await fs.readdir(distPath);
const spaDistFiles = await fs.readdir(clientPath);
if (
!spaDistFiles.some((file) => requestPath.startsWith('/' + file)) &&