Skip to content

Commit 4326c4d

Browse files
Merge branch 'jmcdo29-fix/mve-options-to-prop-injection'
2 parents 778b0ef + d0ac98f commit 4326c4d

14 files changed

+8184
-2110
lines changed

.circleci/config.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ jobs:
3434
- ./node_modules
3535
- run:
3636
name: Build
37-
command: npm run build
37+
command: npm run build
38+
- run:
39+
name: Test
40+
command: npm run test
3841

3942
workflows:
4043
version: 2

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ npm-debug.log
1212
.DS_Store
1313

1414
# tests
15-
/test
1615
/coverage
1716
/.nyc_output
1817

jest.config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
moduleFileExtensions: ['js', 'json', 'ts'],
3+
rootDir: '.',
4+
testMatch: ['<rootDir>/test/*.e2e-spec.ts'],
5+
transform: {
6+
'^.+\\.ts$': 'ts-jest'
7+
},
8+
testEnvironment: 'node',
9+
collectCoverage: true
10+
};

lib/auth.guard.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import {
33
CanActivate,
44
ExecutionContext,
5+
Inject,
56
Logger,
67
mixin,
78
Optional,
@@ -23,16 +24,17 @@ export type IAuthGuard = CanActivate & {
2324
handleRequest<TUser = any>(err, user, info, context, status?): TUser;
2425
getAuthenticateOptions(context): IAuthModuleOptions | undefined;
2526
};
26-
export const AuthGuard: (
27-
type?: string | string[]
28-
) => Type<IAuthGuard> = memoize(createAuthGuard);
27+
export const AuthGuard: (type?: string | string[]) => Type<IAuthGuard> =
28+
memoize(createAuthGuard);
2929

3030
const NO_STRATEGY_ERROR = `In order to use "defaultStrategy", please, ensure to import PassportModule in each place where AuthGuard() is being used. Otherwise, passport won't work correctly.`;
3131

3232
function createAuthGuard(type?: string | string[]): Type<CanActivate> {
3333
class MixinAuthGuard<TUser = any> implements CanActivate {
34-
constructor(@Optional() protected readonly options?: AuthModuleOptions) {
35-
this.options = this.options || {};
34+
@Inject(AuthModuleOptions)
35+
protected options: AuthModuleOptions;
36+
constructor(@Optional() options?: AuthModuleOptions) {
37+
this.options = options ?? this.options;
3638
if (!type && !this.options.defaultStrategy) {
3739
new Logger('AuthGuard').error(NO_STRATEGY_ERROR);
3840
}
@@ -42,7 +44,7 @@ function createAuthGuard(type?: string | string[]): Type<CanActivate> {
4244
const options = {
4345
...defaultOptions,
4446
...this.options,
45-
...await this.getAuthenticateOptions(context)
47+
...(await this.getAuthenticateOptions(context))
4648
};
4749
const [request, response] = [
4850
this.getRequest(context),
@@ -93,18 +95,15 @@ function createAuthGuard(type?: string | string[]): Type<CanActivate> {
9395
return guard;
9496
}
9597

96-
const createPassportContext = (request, response) => (
97-
type,
98-
options,
99-
callback: Function
100-
) =>
101-
new Promise<void>((resolve, reject) =>
102-
passport.authenticate(type, options, (err, user, info, status) => {
103-
try {
104-
request.authInfo = info;
105-
return resolve(callback(err, user, info, status));
106-
} catch (err) {
107-
reject(err);
108-
}
109-
})(request, response, (err) => (err ? reject(err) : resolve()))
110-
);
98+
const createPassportContext =
99+
(request, response) => (type, options, callback: Function) =>
100+
new Promise<void>((resolve, reject) =>
101+
passport.authenticate(type, options, (err, user, info, status) => {
102+
try {
103+
request.authInfo = info;
104+
return resolve(callback(err, user, info, status));
105+
} catch (err) {
106+
reject(err);
107+
}
108+
})(request, response, (err) => (err ? reject(err) : resolve()))
109+
);

0 commit comments

Comments
 (0)