fix: crashes in user last login interceptor (#4146)

Co-authored-by: Andrew Bastin <andrewbastin.k@gmail.com>
This commit is contained in:
Mir Arif Hasan
2024-06-25 17:45:04 +06:00
committed by GitHub
parent aead9e6c98
commit 190a3b8eaf
2 changed files with 5900 additions and 5569 deletions

View File

@@ -14,8 +14,6 @@ import { UserService } from 'src/user/user.service';
export class UserLastActiveOnInterceptor implements NestInterceptor {
constructor(private userService: UserService) {}
user: AuthUser; // 'user', who executed the request
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
if (context.getType() === 'http') {
return this.restHandler(context, next);
@@ -26,17 +24,17 @@ export class UserLastActiveOnInterceptor implements NestInterceptor {
restHandler(context: ExecutionContext, next: CallHandler): Observable<any> {
const request = context.switchToHttp().getRequest();
this.user = request.user;
const user: AuthUser = request.user;
return next.handle().pipe(
tap(() => {
if (this.user && typeof this.user === 'object') {
this.userService.updateUserLastActiveOn(this.user.uid);
if (user && typeof user === 'object') {
this.userService.updateUserLastActiveOn(user.uid);
}
}),
catchError((error) => {
if (this.user && typeof this.user === 'object') {
this.userService.updateUserLastActiveOn(this.user.uid);
if (user && typeof user === 'object') {
this.userService.updateUserLastActiveOn(user.uid);
}
return throwError(() => error);
}),
@@ -48,17 +46,17 @@ export class UserLastActiveOnInterceptor implements NestInterceptor {
next: CallHandler,
): Observable<any> {
const contextObject = GqlExecutionContext.create(context).getContext();
this.user = contextObject.req.user;
const user: AuthUser = contextObject?.req?.user;
return next.handle().pipe(
tap(() => {
if (this.user && typeof this.user === 'object') {
this.userService.updateUserLastActiveOn(this.user.uid);
if (user && typeof user === 'object') {
this.userService.updateUserLastActiveOn(user.uid);
}
}),
catchError((error) => {
if (this.user && typeof this.user === 'object') {
this.userService.updateUserLastActiveOn(this.user.uid);
if (user && typeof user === 'object') {
this.userService.updateUserLastActiveOn(user.uid);
}
return throwError(() => error);
}),