Skip to content

Commit

Permalink
feat(express): support custom auth routes prefix (#538)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangsijie authored Aug 10, 2023
1 parent dedeaae commit 27f6a53
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/dull-scissors-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@logto/express": minor
---

Support custom auth routes prefix
8 changes: 8 additions & 0 deletions packages/express/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
7 changes: 4 additions & 3 deletions packages/express/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions packages/express/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ declare module 'http' {

export type LogtoExpressConfig = LogtoConfig & {
baseUrl: string;
authRoutesPrefix?: string;
} & GetContextParameters;

0 comments on commit 27f6a53

Please sign in to comment.