Skip to content

Commit df3e225

Browse files
Address CodeRabbit review feedback
- Reset cached init promise on failure so transient startup errors recover - Handle array/missing state query values in isSignOutSuccess (Express 5) - Replace Jest types and drop dead include entries in tsconfig.spec.json - Clarify sign-in redirect behavior in README and service docs
1 parent 8f236e8 commit df3e225

4 files changed

Lines changed: 31 additions & 17 deletions

File tree

‎packages/nestjs/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ export class AuthController {
5454
if (tokens.accessToken || tokens.idToken) {
5555
res.redirect('/');
5656
}
57+
// If no tokens were returned, signIn has already redirected the
58+
// response to the ThunderID sign-in page.
5759
}
5860

5961
@Get('logout')

‎packages/nestjs/src/ThunderIDService.ts‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ import {ThunderIDNestConfig} from './models/config';
4343
* async login(@Req() req: Request, @Res() res: Response) {
4444
* const tokens = await this.thunderID.signIn(req, res);
4545
* if (tokens.accessToken || tokens.idToken) res.redirect('/');
46+
* // If no tokens were returned, signIn has already redirected the
47+
* // response to the ThunderID sign-in page.
4648
* }
4749
* }
4850
* ```
@@ -69,11 +71,18 @@ class ThunderIDService {
6971
private ensureInitialized(req: express.Request): Promise<boolean> {
7072
if (this.initPromise === undefined) {
7173
const origin = `${req.protocol}://${req.get('host')}`;
72-
this.initPromise = this.client.initialize({
73-
...this.config,
74-
afterSignInUrl: this.config.afterSignInUrl ?? `${origin}/login`,
75-
afterSignOutUrl: this.config.afterSignOutUrl ?? `${origin}/logout`,
76-
});
74+
this.initPromise = this.client
75+
.initialize({
76+
...this.config,
77+
afterSignInUrl: this.config.afterSignInUrl ?? `${origin}/login`,
78+
afterSignOutUrl: this.config.afterSignOutUrl ?? `${origin}/logout`,
79+
})
80+
.catch((error: unknown) => {
81+
// Clear the cached promise so a transient startup failure doesn't
82+
// break the service until restart.
83+
this.initPromise = undefined;
84+
throw error;
85+
});
7786
}
7887
return this.initPromise;
7988
}
@@ -123,7 +132,8 @@ class ThunderIDService {
123132

124133
/** Whether the request is the identity provider's redirect back after a completed sign-out. */
125134
public isSignOutSuccess(req: express.Request): boolean {
126-
return req.query['state'] === 'sign_out_success';
135+
const state: unknown = req.query['state'];
136+
return typeof state === 'string' && state === 'sign_out_success';
127137
}
128138

129139
/** Whether the request carries a valid ThunderID session. */

‎packages/nestjs/src/__tests__/ThunderIDGuard.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ describe('ThunderIDGuard', () => {
5252
});
5353
});
5454

55+
describe('ThunderIDService.isSignOutSuccess', () => {
56+
it('handles string, array, and missing state query values', () => {
57+
const service = Object.create(ThunderIDService.prototype) as ThunderIDService;
58+
const requestWithState = (state: unknown): ThunderIDRequest => ({query: {state}}) as unknown as ThunderIDRequest;
59+
60+
expect(service.isSignOutSuccess(requestWithState('sign_out_success'))).toBe(true);
61+
expect(service.isSignOutSuccess(requestWithState(['sign_out_success', 'other']))).toBe(false);
62+
expect(service.isSignOutSuccess(requestWithState(undefined))).toBe(false);
63+
});
64+
});
65+
5566
describe('ThunderIDModule', () => {
5667
it('forRoot registers the config provider, service, and guard globally', () => {
5768
const config = {clientId: 'client-1'} as unknown as ThunderIDNestConfig;

‎packages/nestjs/tsconfig.spec.json‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,7 @@
33
"compilerOptions": {
44
"outDir": "dist",
55
"module": "commonjs",
6-
"types": ["jest", "node"]
6+
"types": ["node"]
77
},
8-
"include": [
9-
"test-configs",
10-
"vitest.config.ts",
11-
"jest.config.js",
12-
"**/*.test.ts",
13-
"**/*.spec.ts",
14-
"**/*.test.js",
15-
"**/*.spec.js",
16-
"**/*.d.ts"
17-
]
8+
"include": ["vitest.config.ts", "**/*.test.ts", "**/*.spec.ts", "**/*.d.ts"]
189
}

0 commit comments

Comments
 (0)