Skip to content

Commit

Permalink
Merge branch 'jmcdo29-fix/mve-options-to-prop-injection'
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Feb 14, 2022
2 parents 778b0ef + d0ac98f commit 4326c4d
Show file tree
Hide file tree
Showing 14 changed files with 8,184 additions and 2,110 deletions.
5 changes: 4 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ jobs:
- ./node_modules
- run:
name: Build
command: npm run build
command: npm run build
- run:
name: Test
command: npm run test

workflows:
version: 2
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ npm-debug.log
.DS_Store

# tests
/test
/coverage
/.nyc_output

Expand Down
10 changes: 10 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
moduleFileExtensions: ['js', 'json', 'ts'],
rootDir: '.',
testMatch: ['<rootDir>/test/*.e2e-spec.ts'],
transform: {
'^.+\\.ts$': 'ts-jest'
},
testEnvironment: 'node',
collectCoverage: true
};
41 changes: 20 additions & 21 deletions lib/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import {
CanActivate,
ExecutionContext,
Inject,
Logger,
mixin,
Optional,
Expand All @@ -23,16 +24,17 @@ export type IAuthGuard = CanActivate & {
handleRequest<TUser = any>(err, user, info, context, status?): TUser;
getAuthenticateOptions(context): IAuthModuleOptions | undefined;
};
export const AuthGuard: (
type?: string | string[]
) => Type<IAuthGuard> = memoize(createAuthGuard);
export const AuthGuard: (type?: string | string[]) => Type<IAuthGuard> =
memoize(createAuthGuard);

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.`;

function createAuthGuard(type?: string | string[]): Type<CanActivate> {
class MixinAuthGuard<TUser = any> implements CanActivate {
constructor(@Optional() protected readonly options?: AuthModuleOptions) {
this.options = this.options || {};
@Inject(AuthModuleOptions)
protected options: AuthModuleOptions;
constructor(@Optional() options?: AuthModuleOptions) {
this.options = options ?? this.options;
if (!type && !this.options.defaultStrategy) {
new Logger('AuthGuard').error(NO_STRATEGY_ERROR);
}
Expand All @@ -42,7 +44,7 @@ function createAuthGuard(type?: string | string[]): Type<CanActivate> {
const options = {
...defaultOptions,
...this.options,
...await this.getAuthenticateOptions(context)
...(await this.getAuthenticateOptions(context))
};
const [request, response] = [
this.getRequest(context),
Expand Down Expand Up @@ -93,18 +95,15 @@ function createAuthGuard(type?: string | string[]): Type<CanActivate> {
return guard;
}

const createPassportContext = (request, response) => (
type,
options,
callback: Function
) =>
new Promise<void>((resolve, reject) =>
passport.authenticate(type, options, (err, user, info, status) => {
try {
request.authInfo = info;
return resolve(callback(err, user, info, status));
} catch (err) {
reject(err);
}
})(request, response, (err) => (err ? reject(err) : resolve()))
);
const createPassportContext =
(request, response) => (type, options, callback: Function) =>
new Promise<void>((resolve, reject) =>
passport.authenticate(type, options, (err, user, info, status) => {
try {
request.authInfo = info;
return resolve(callback(err, user, info, status));
} catch (err) {
reject(err);
}
})(request, response, (err) => (err ? reject(err) : resolve()))
);
Loading

0 comments on commit 4326c4d

Please sign in to comment.