Skip to content

Instantly share code, notes, and snippets.

@DScheglov
Last active November 16, 2020 13:12
Show Gist options
  • Save DScheglov/ffc8f7524cb0e4debd879099eec150d2 to your computer and use it in GitHub Desktop.
Save DScheglov/ffc8f7524cb0e4debd879099eec150d2 to your computer and use it in GitHub Desktop.
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