11import { CanActivate , ExecutionContext , UnauthorizedException } from '@nestjs/common' ;
2- import passport , { AuthenticateOptions } from 'passport' ;
2+ import * as passport from 'passport' ;
33
44export const scopes = new Map ( ) ;
55
6- export interface IAuthGuardOptions extends AuthenticateOptions {
6+ export interface IAuthGuardOptions extends passport . AuthenticateOptions {
77 isAuth ?: boolean ;
88 strategy : string | string [ ] ;
99}
@@ -17,7 +17,7 @@ export class AuthGuard implements CanActivate {
1717 if ( typeof this . options . scope === 'string' ) {
1818 this . options . scope = [ this . options . scope ] ;
1919 }
20- if ( this . options . scope ) {
20+ if ( Array . isArray ( this . options . scope ) ) {
2121 for ( const scope of this . options . scope ) {
2222 scopes . set ( scope , scope ) ;
2323 }
@@ -32,7 +32,7 @@ export class AuthGuard implements CanActivate {
3232 let [ request , response , next ] = [ httpCtx . getRequest ( ) , httpCtx . getResponse ( ) , httpCtx . getNext ( ) ] ;
3333
3434 // support graphql context
35- if ( ! request ) {
35+ if ( typeof request !== 'object' ) {
3636 request = context . getArgByIndex ( 2 ) . request ;
3737 response = request . res ;
3838 next = ( err ) => {
@@ -41,27 +41,27 @@ export class AuthGuard implements CanActivate {
4141 }
4242
4343 for ( const strategy of this . options . strategy ) {
44- if ( strategy === 'local' && request . isAuthenticated ( ) ) {
44+ if ( strategy === 'local' && request . isAuthenticated ( ) === true ) {
4545 return true ;
4646 } else {
47- await new Promise ( ( ok , fail ) =>
47+ await new Promise ( ( resolve , reject ) =>
4848 passport . authenticate ( strategy , this . options , async ( err , user , info ) => {
4949 request . authInfo = info ;
5050 try {
51- if ( ! user && this . options . isAuth ) {
51+ if ( typeof user !== 'object' && this . options . isAuth ) {
5252 throw new Error ( 'User not found' ) ;
5353 }
54- if ( err ) {
54+ if ( err !== undefined || err !== null ) {
5555 throw err ;
5656 }
57- if ( user ) {
57+ if ( typeof user === 'object' ) {
5858 await this . login ( user , request ) ;
5959 }
60- ok ( ) ;
60+ resolve ( ) ;
6161 } catch ( e ) {
6262 // TO DO we could simply return false here to let the canActivate do the job
6363 // And just Log the error
64- fail ( new UnauthorizedException ( e . message . message || e . message || 'Uncaught error' ) ) ;
64+ reject ( new UnauthorizedException ( typeof e . message === 'string' ? e . message : 'Uncaught error' ) ) ;
6565 }
6666 } ) ( request , response , next )
6767 ) ;
@@ -73,12 +73,12 @@ export class AuthGuard implements CanActivate {
7373
7474 async login < User > ( user : any , request : any ) : Promise < User > {
7575 request . oauth2Scope = this . options . scope ;
76- await new Promise ( ( ok , fail ) => {
76+ await new Promise ( ( resolve , reject ) => {
7777 request . logIn ( user , ( loginErr : Error ) => {
78- if ( loginErr ) {
79- return fail ( loginErr ) ;
78+ if ( loginErr instanceof Error ) {
79+ return reject ( loginErr ) ;
8080 }
81- ok ( ) ;
81+ resolve ( ) ;
8282 } ) ;
8383 } ) ;
8484 return user ;
0 commit comments