From 27f6a533801535f2a169eb752e0063b0199d3b93 Mon Sep 17 00:00:00 2001 From: wangsijie Date: Thu, 10 Aug 2023 10:21:13 +0800 Subject: [PATCH] feat(express): support custom auth routes prefix (#538) --- .changeset/dull-scissors-walk.md | 5 +++++ packages/express/src/index.test.ts | 8 ++++++++ packages/express/src/index.ts | 7 ++++--- packages/express/src/types.ts | 1 + 4 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 .changeset/dull-scissors-walk.md diff --git a/.changeset/dull-scissors-walk.md b/.changeset/dull-scissors-walk.md new file mode 100644 index 00000000..71f1473c --- /dev/null +++ b/.changeset/dull-scissors-walk.md @@ -0,0 +1,5 @@ +--- +"@logto/express": minor +--- + +Support custom auth routes prefix diff --git a/packages/express/src/index.test.ts b/packages/express/src/index.test.ts index 6f42c556..509c6845 100644 --- a/packages/express/src/index.test.ts +++ b/packages/express/src/index.test.ts @@ -63,6 +63,14 @@ describe('Express', () => { expect(response.header.location).toEqual(signInUrl); expect(signIn).toHaveBeenCalled(); }); + + it('should support custom auth routes prefix', async () => { + const response = await testRouter( + handleAuthRoutes({ ...configs, authRoutesPrefix: 'custom' }) + ).get('/custom/sign-in'); + expect(response.header.location).toEqual(signInUrl); + expect(signIn).toHaveBeenCalled(); + }); }); describe('handleSignUn', () => { diff --git a/packages/express/src/index.ts b/packages/express/src/index.ts index fcf3e85e..f94d9ce0 100644 --- a/packages/express/src/index.ts +++ b/packages/express/src/index.ts @@ -43,20 +43,21 @@ const createNodeClient = ( export const handleAuthRoutes = (config: LogtoExpressConfig): Router => { // eslint-disable-next-line new-cap const router = Router(); + const prefix = config.authRoutesPrefix ?? 'logto'; - router.use('/logto/:action', async (request, response) => { + router.use(`/${prefix}/:action`, async (request, response) => { const { action } = request.params; const nodeClient = createNodeClient(request, response, config); switch (action) { case 'sign-in': { - await nodeClient.signIn(`${config.baseUrl}/logto/sign-in-callback`); + await nodeClient.signIn(`${config.baseUrl}/${prefix}/sign-in-callback`); break; } case 'sign-up': { - await nodeClient.signIn(`${config.baseUrl}/logto/sign-in-callback`, 'signUp'); + await nodeClient.signIn(`${config.baseUrl}/${prefix}/sign-in-callback`, 'signUp'); break; } diff --git a/packages/express/src/types.ts b/packages/express/src/types.ts index d97adca8..1a015aab 100644 --- a/packages/express/src/types.ts +++ b/packages/express/src/types.ts @@ -10,4 +10,5 @@ declare module 'http' { export type LogtoExpressConfig = LogtoConfig & { baseUrl: string; + authRoutesPrefix?: string; } & GetContextParameters;