22import {
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
3030const 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
3232function 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