cr
This commit is contained in:
@@ -2,14 +2,14 @@ import {
|
||||
Injectable,
|
||||
UnauthorizedException,
|
||||
ConflictException,
|
||||
} from '@nestjs/common';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
import * as crypto from 'crypto';
|
||||
import { PrismaService } from '../../database/prisma.service';
|
||||
import { RegisterDto, LoginDto, TokenResponseDto } from './dto/auth.dto';
|
||||
import { User, UserRole } from '@prisma/client';
|
||||
} from "@nestjs/common";
|
||||
import { JwtService } from "@nestjs/jwt";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import * as bcrypt from "bcrypt";
|
||||
import * as crypto from "crypto";
|
||||
import { PrismaService } from "../../database/prisma.service";
|
||||
import { RegisterDto, LoginDto, TokenResponseDto } from "./dto/auth.dto";
|
||||
import { User, UserRole } from "@prisma/client";
|
||||
|
||||
export interface JwtPayload {
|
||||
sub: string;
|
||||
@@ -36,7 +36,7 @@ export class AuthService {
|
||||
});
|
||||
|
||||
if (existingUser) {
|
||||
throw new ConflictException('EMAIL_ALREADY_EXISTS');
|
||||
throw new ConflictException("EMAIL_ALREADY_EXISTS");
|
||||
}
|
||||
|
||||
// Hash password
|
||||
@@ -76,7 +76,7 @@ export class AuthService {
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new UnauthorizedException('INVALID_CREDENTIALS');
|
||||
throw new UnauthorizedException("INVALID_CREDENTIALS");
|
||||
}
|
||||
|
||||
// Verify password
|
||||
@@ -86,11 +86,11 @@ export class AuthService {
|
||||
);
|
||||
|
||||
if (!isPasswordValid) {
|
||||
throw new UnauthorizedException('INVALID_CREDENTIALS');
|
||||
throw new UnauthorizedException("INVALID_CREDENTIALS");
|
||||
}
|
||||
|
||||
if (!user.isActive) {
|
||||
throw new UnauthorizedException('ACCOUNT_DISABLED');
|
||||
throw new UnauthorizedException("ACCOUNT_DISABLED");
|
||||
}
|
||||
|
||||
return this.generateTokens(user);
|
||||
@@ -109,7 +109,7 @@ export class AuthService {
|
||||
});
|
||||
|
||||
if (!storedToken) {
|
||||
throw new UnauthorizedException('INVALID_REFRESH_TOKEN');
|
||||
throw new UnauthorizedException("INVALID_REFRESH_TOKEN");
|
||||
}
|
||||
|
||||
if (storedToken.expiresAt < new Date()) {
|
||||
@@ -117,7 +117,7 @@ export class AuthService {
|
||||
await this.prisma.refreshToken.delete({
|
||||
where: { id: storedToken.id },
|
||||
});
|
||||
throw new UnauthorizedException('INVALID_REFRESH_TOKEN');
|
||||
throw new UnauthorizedException("INVALID_REFRESH_TOKEN");
|
||||
}
|
||||
|
||||
// Delete old refresh token
|
||||
@@ -167,13 +167,13 @@ export class AuthService {
|
||||
|
||||
// Generate access token
|
||||
const accessToken = this.jwtService.sign(payload, {
|
||||
expiresIn: this.configService.get('JWT_ACCESS_EXPIRATION', '15m'),
|
||||
expiresIn: this.configService.get("JWT_ACCESS_EXPIRATION", "15m"),
|
||||
});
|
||||
|
||||
// Generate refresh token
|
||||
const refreshTokenValue = crypto.randomUUID();
|
||||
const refreshExpiration = this.parseExpiration(
|
||||
this.configService.get('JWT_REFRESH_EXPIRATION', '7d'),
|
||||
this.configService.get("JWT_REFRESH_EXPIRATION", "7d"),
|
||||
);
|
||||
|
||||
// Store refresh token
|
||||
@@ -190,7 +190,7 @@ export class AuthService {
|
||||
refreshToken: refreshTokenValue,
|
||||
expiresIn:
|
||||
this.parseExpiration(
|
||||
this.configService.get('JWT_ACCESS_EXPIRATION', '15m'),
|
||||
this.configService.get("JWT_ACCESS_EXPIRATION", "15m"),
|
||||
) / 1000, // Convert to seconds
|
||||
user: {
|
||||
id: user.id,
|
||||
@@ -233,13 +233,13 @@ export class AuthService {
|
||||
const unit = match[2];
|
||||
|
||||
switch (unit) {
|
||||
case 's':
|
||||
case "s":
|
||||
return value * 1000;
|
||||
case 'm':
|
||||
case "m":
|
||||
return value * 60 * 1000;
|
||||
case 'h':
|
||||
case "h":
|
||||
return value * 60 * 60 * 1000;
|
||||
case 'd':
|
||||
case "d":
|
||||
return value * 24 * 60 * 60 * 1000;
|
||||
default:
|
||||
return 15 * 60 * 1000;
|
||||
|
||||
Reference in New Issue
Block a user