Last active
November 16, 2020 13:12
-
-
Save DScheglov/ffc8f7524cb0e4debd879099eec150d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common'; | |
import { Observable, Observer } from 'rxjs'; | |
import { flatMap } from 'rxjs/operators'; | |
const nextAndComplete = <T>(observer: Observer<T>, value?: T): void => { | |
observer.next(result); | |
observer.complete(); | |
}; | |
@Injectable() | |
export class LoginInterceptor implements NestInterceptor { | |
intercept(context: ExecutionContext, next: CallHandler): Observable<any> { | |
const req = context.switchToHttp().getRequest(); | |
return new Observable(observer => | |
req.logIn(req.user, err => err ? observer.error(err) : nextAndComplete(observer)) | |
)).pipe( | |
flatMap(() => next.handle()) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment