This commit is contained in:
2025-05-13 02:20:31 +07:00
parent 6e9bfa4b82
commit 87a7eb0931
3 changed files with 137 additions and 130 deletions

View File

@@ -4,6 +4,8 @@ import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm'; import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config'; import { ConfigModule, ConfigService } from '@nestjs/config';
import * as Joi from 'joi'; import * as Joi from 'joi';
import {UsersModule} from "./users/user.module";
import {AuthenticationModule} from "./authentication/authentication.module";
@Module({ @Module({
imports: [ imports: [
@@ -17,6 +19,7 @@ import * as Joi from 'joi';
username: configService.get<string>('POSTGRES_USER'), username: configService.get<string>('POSTGRES_USER'),
password: configService.get<string>('POSTGRES_PASSWORD'), password: configService.get<string>('POSTGRES_PASSWORD'),
database: configService.get<string>('POSTGRES_DB'), database: configService.get<string>('POSTGRES_DB'),
autoLoadEntities: true,
ssl: { ssl: {
rejectUnauthorized: false, // Needed for Neon and similar managed DBs rejectUnauthorized: false, // Needed for Neon and similar managed DBs
}, },
@@ -28,7 +31,9 @@ import * as Joi from 'joi';
JWT_SECRET: Joi.string().required(), JWT_SECRET: Joi.string().required(),
JWT_EXPIRATION_TIME: Joi.string().required(), JWT_EXPIRATION_TIME: Joi.string().required(),
}) })
}) }),
UsersModule,
AuthenticationModule
], ],
controllers: [AppController], controllers: [AppController],
providers: [AppService], providers: [AppService],

View File

@@ -1,10 +1,10 @@
import { Module } from '@nestjs/common'; import {Module} from '@nestjs/common';
import { AuthenticationService } from './authentication.service'; import {AuthenticationService} from './authentication.service';
import { AuthenticationController } from './authentication.controller'; import {AuthenticationController} from './authentication.controller';
import { PassportModule } from '@nestjs/passport'; import {PassportModule} from '@nestjs/passport';
import { LocalStrategy } from './local.strategy'; import {LocalStrategy} from './local.strategy';
import { ConfigModule, ConfigService } from '@nestjs/config'; import {ConfigModule, ConfigService} from '@nestjs/config';
import { JwtModule } from '@nestjs/jwt'; import {JwtModule} from '@nestjs/jwt';
import {JwtStrategy} from "./jwt.strategy"; import {JwtStrategy} from "./jwt.strategy";
import {UsersModule} from "../users/user.module"; import {UsersModule} from "../users/user.module";
@@ -27,4 +27,5 @@ import {UsersModule} from "../users/user.module";
providers: [AuthenticationService, LocalStrategy, JwtStrategy] as const, providers: [AuthenticationService, LocalStrategy, JwtStrategy] as const,
controllers: [AuthenticationController] as const, controllers: [AuthenticationController] as const,
}) })
export class AuthenticationModule {} export class AuthenticationModule {
}

View File

@@ -1,8 +1,8 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; import {HttpException, HttpStatus, Injectable} from '@nestjs/common';
import RegisterDto from './dto/register.dto'; import RegisterDto from './dto/register.dto';
import * as bcrypt from 'bcrypt'; import * as bcrypt from 'bcrypt';
import { JwtService } from '@nestjs/jwt'; import {JwtService} from '@nestjs/jwt';
import { ConfigService } from '@nestjs/config'; import {ConfigService} from '@nestjs/config';
import {UsersService} from "../users/user.service"; import {UsersService} from "../users/user.service";
import PostgresErrorCode from 'src/database/postgresErrorCodes.enum'; import PostgresErrorCode from 'src/database/postgresErrorCodes.enum';
@@ -12,7 +12,8 @@ export class AuthenticationService {
private readonly usersService: UsersService, private readonly usersService: UsersService,
private readonly jwtService: JwtService, private readonly jwtService: JwtService,
private readonly configService: ConfigService, private readonly configService: ConfigService,
) {} ) {
}
public async register(registrationData: RegisterDto) { public async register(registrationData: RegisterDto) {
const hashedPassword = await bcrypt.hash(registrationData.password, 10); const hashedPassword = await bcrypt.hash(registrationData.password, 10);
@@ -30,6 +31,7 @@ export class AuthenticationService {
HttpStatus.BAD_REQUEST, HttpStatus.BAD_REQUEST,
); );
} }
console.log(error);
throw new HttpException( throw new HttpException(
'Something went wrong', 'Something went wrong',
HttpStatus.INTERNAL_SERVER_ERROR, HttpStatus.INTERNAL_SERVER_ERROR,
@@ -41,7 +43,7 @@ export class AuthenticationService {
userId: number, userId: number,
isSecondFactorAuthenticated = false, isSecondFactorAuthenticated = false,
) { ) {
const payload: TokenPayload = { userId, isSecondFactorAuthenticated }; const payload: TokenPayload = {userId, isSecondFactorAuthenticated};
const token = this.jwtService.sign(payload, { const token = this.jwtService.sign(payload, {
secret: this.configService.get('JWT_ACCESS_TOKEN_SECRET'), secret: this.configService.get('JWT_ACCESS_TOKEN_SECRET'),
expiresIn: `${this.configService.get( expiresIn: `${this.configService.get(
@@ -54,12 +56,11 @@ export class AuthenticationService {
} }
public getCookieWithJwtRefreshToken(userId: number) { public getCookieWithJwtRefreshToken(userId: number) {
const payload: TokenPayload = { userId }; const payload: TokenPayload = {userId};
const token = this.jwtService.sign(payload, { const token = this.jwtService.sign(payload, {
secret: this.configService.get('JWT_REFRESH_TOKEN_SECRET'), secret: this.configService.get('JWT_REFRESH_TOKEN_SECRET'),
expiresIn: `${this.configService.get( expiresIn: this.configService.get('JWT_ACCESS_TOKEN_EXPIRATION_TIME'),
'JWT_REFRESH_TOKEN_EXPIRATION_TIME',
)}s`,
}); });
const cookie = `Refresh=${token}; HttpOnly; Path=/; Max-Age=${this.configService.get( const cookie = `Refresh=${token}; HttpOnly; Path=/; Max-Age=${this.configService.get(
'JWT_REFRESH_TOKEN_EXPIRATION_TIME', 'JWT_REFRESH_TOKEN_EXPIRATION_TIME',