mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 10:25:23 +00:00
Merge branch 'dev' of https://github.com/SyncrowIOT/backend into dev
This commit is contained in:
@ -33,32 +33,33 @@ export class AuthService {
|
|||||||
const user = await this.userRepository.findOne({
|
const user = await this.userRepository.findOne({
|
||||||
where: {
|
where: {
|
||||||
email,
|
email,
|
||||||
region: regionUuid
|
region: regionUuid ? { uuid: regionUuid } : undefined,
|
||||||
? {
|
|
||||||
uuid: regionUuid,
|
|
||||||
}
|
|
||||||
: undefined,
|
|
||||||
},
|
},
|
||||||
relations: ['roleType'],
|
relations: ['roleType'],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
throw new BadRequestException('Invalid credentials');
|
||||||
|
}
|
||||||
|
|
||||||
if (!user.isUserVerified) {
|
if (!user.isUserVerified) {
|
||||||
throw new BadRequestException('User is not verified');
|
throw new BadRequestException('User is not verified');
|
||||||
}
|
}
|
||||||
if (!user.isActive) {
|
if (!user.isActive) {
|
||||||
throw new BadRequestException('User is not active');
|
throw new BadRequestException('User is not active');
|
||||||
}
|
}
|
||||||
if (user) {
|
|
||||||
const passwordMatch = this.helperHashService.bcryptCompare(
|
const passwordMatch = await this.helperHashService.bcryptCompare(
|
||||||
pass,
|
pass,
|
||||||
user.password,
|
user.password,
|
||||||
);
|
);
|
||||||
if (passwordMatch) {
|
if (!passwordMatch) {
|
||||||
const { ...result } = user;
|
throw new BadRequestException('Invalid credentials');
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
const { password, ...result } = user;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
async createSession(data): Promise<UserSessionEntity> {
|
async createSession(data): Promise<UserSessionEntity> {
|
||||||
|
@ -5,34 +5,55 @@ import {
|
|||||||
HttpException,
|
HttpException,
|
||||||
HttpStatus,
|
HttpStatus,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { Response } from 'express';
|
import { Request, Response } from 'express';
|
||||||
|
|
||||||
@Catch()
|
@Catch()
|
||||||
export class HttpExceptionFilter implements ExceptionFilter {
|
export class HttpExceptionFilter implements ExceptionFilter {
|
||||||
catch(exception: unknown, host: ArgumentsHost) {
|
catch(exception: unknown, host: ArgumentsHost): void {
|
||||||
const ctx = host.switchToHttp();
|
const ctx = host.switchToHttp();
|
||||||
const response = ctx.getResponse<Response>();
|
const response = ctx.getResponse<Response>();
|
||||||
const request = ctx.getRequest<Request>();
|
const request = ctx.getRequest<Request>();
|
||||||
const status =
|
|
||||||
exception instanceof HttpException
|
|
||||||
? exception.getStatus()
|
|
||||||
: HttpStatus.INTERNAL_SERVER_ERROR;
|
|
||||||
|
|
||||||
const message =
|
const status = this.getStatus(exception);
|
||||||
exception instanceof HttpException
|
const errorMessage = this.getErrorMessage(exception);
|
||||||
? exception.getResponse()
|
const formattedStatus = this.formatStatus(status);
|
||||||
: 'Internal server error';
|
|
||||||
|
|
||||||
const errorResponse = {
|
const errorResponse = {
|
||||||
statusCode: status,
|
statusCode: status,
|
||||||
timestamp: new Date().toISOString(),
|
timestamp: new Date().toISOString(),
|
||||||
path: request.url,
|
path: request.url,
|
||||||
error: message,
|
error:
|
||||||
|
typeof errorMessage === 'string'
|
||||||
|
? {
|
||||||
|
message: errorMessage,
|
||||||
|
error: formattedStatus,
|
||||||
|
statusCode: status,
|
||||||
|
}
|
||||||
|
: errorMessage,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Optionally log the exception
|
console.error('Error occurred:', exception);
|
||||||
console.error(`Error occurred:`, exception);
|
|
||||||
|
|
||||||
response.status(status).json(errorResponse);
|
response.status(status).json(errorResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private getStatus(exception: unknown): HttpStatus {
|
||||||
|
return exception instanceof HttpException
|
||||||
|
? exception.getStatus()
|
||||||
|
: HttpStatus.INTERNAL_SERVER_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
private getErrorMessage(exception: unknown): string | object {
|
||||||
|
return exception instanceof HttpException
|
||||||
|
? exception.getResponse()
|
||||||
|
: 'Internal server error';
|
||||||
|
}
|
||||||
|
|
||||||
|
private formatStatus(status: HttpStatus): string {
|
||||||
|
return HttpStatus[status]
|
||||||
|
.toLowerCase()
|
||||||
|
.replace('_', ' ')
|
||||||
|
.split(' ')
|
||||||
|
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
|
||||||
|
.join(' ');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user