2
2
import {
3
3
CanActivate ,
4
4
ExecutionContext ,
5
+ Inject ,
5
6
Logger ,
6
7
mixin ,
7
8
Optional ,
@@ -23,16 +24,17 @@ export type IAuthGuard = CanActivate & {
23
24
handleRequest < TUser = any > ( err , user , info , context , status ?) : TUser ;
24
25
getAuthenticateOptions ( context ) : IAuthModuleOptions | undefined ;
25
26
} ;
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 ) ;
29
29
30
30
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.` ;
31
31
32
32
function createAuthGuard ( type ?: string | string [ ] ) : Type < CanActivate > {
33
33
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 ;
36
38
if ( ! type && ! this . options . defaultStrategy ) {
37
39
new Logger ( 'AuthGuard' ) . error ( NO_STRATEGY_ERROR ) ;
38
40
}
@@ -42,7 +44,7 @@ function createAuthGuard(type?: string | string[]): Type<CanActivate> {
42
44
const options = {
43
45
...defaultOptions ,
44
46
...this . options ,
45
- ...await this . getAuthenticateOptions ( context )
47
+ ...( await this . getAuthenticateOptions ( context ) )
46
48
} ;
47
49
const [ request , response ] = [
48
50
this . getRequest ( context ) ,
@@ -93,18 +95,15 @@ function createAuthGuard(type?: string | string[]): Type<CanActivate> {
93
95
return guard ;
94
96
}
95
97
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