Skip to content

Commit

Permalink
Add better error handling and logs to super admin auth (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
eleanorreem committed Apr 25, 2023
1 parent 5eba245 commit cae303a
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions src/partner-admin/super-admin-auth.guard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { CanActivate, ExecutionContext, Injectable, UnauthorizedException } from '@nestjs/common';
import {
CanActivate,
ExecutionContext,
HttpException,
HttpStatus,
Injectable,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Request } from 'express';
import { AuthService } from '../auth/auth.service';
Expand All @@ -17,13 +23,34 @@ export class SuperAdminAuthGuard implements CanActivate {
const { authorization } = request.headers;

if (!authorization) {
throw new UnauthorizedException('Unauthorized: missing required Authorization token');
throw new HttpException(
`SuperAdminAuthGuard: Unauthorised missing Authorization token`,
HttpStatus.UNAUTHORIZED,
);
}
let userUid;
try {
const { uid } = await this.authService.parseAuth(authorization);
userUid = uid;
} catch (error) {
if (error.code === 'auth/id-token-expired') {
throw new HttpException(`SuperAdminAuthGuard - ${error}`, HttpStatus.UNAUTHORIZED);
}

const { uid } = await this.authService.parseAuth(authorization);

const user = await this.usersRepository.findOne({ firebaseUid: uid });
throw new HttpException(
`SuperAdminAuthGuard - Error parsing firebase user: ${error}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
try {
const user = await this.usersRepository.findOne({ firebaseUid: userUid });

return !!user.isSuperAdmin && user.email.indexOf('@chayn.co') !== -1;
return !!user.isSuperAdmin && user.email.indexOf('@chayn.co') !== -1;
} catch (error) {
throw new HttpException(
`SuperAdminAuthGuard - Error finding user: ${error}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}

0 comments on commit cae303a

Please sign in to comment.