diff --git a/.env b/.env index f60a9b4..4d3d1de 100644 --- a/.env +++ b/.env @@ -1 +1,9 @@ -#PROMETHEUS_API=http://192.168.2.34:9090/api/v1 \ No newline at end of file +#Прометеус +#PROMETHEUS_API=http://192.168.2.34:9090/api/v1 + +#Постгресс +#DB_HOST=192.168.2.37 +#DB_PORT=5432 +#DB_USER=trust +#DB_PASSWORD=kaiqolzp2a4aH +#DB_NAME=trust-db \ No newline at end of file diff --git a/package.json b/package.json index 5e92a63..0fe1fec 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "test:e2e": "jest --config ./test/jest-e2e.json" }, "dependencies": { - "@nestjs/axios": "^4.0.0", + "@nestjs/axios": "^4.0.0", "@nestjs/common": "^11.0.1", "@nestjs/core": "^11.0.1", "@nestjs/config": "^4.0.0", @@ -28,7 +28,12 @@ "axios": "^1.7.9", "reflect-metadata": "^0.2.2", "dotenv": "^16.3.1", - "rxjs": "^7.8.1" + "rxjs": "^7.8.1", + "@nestjs/typeorm": "^11.0.0", + "pg": "^8.14.1", + "typeorm": "^0.3.21", + "bcrypt": "^5.1.1", + "@types/bcrypt": "^5.0.2" }, "devDependencies": { "@eslint/eslintrc": "^3.2.0", diff --git a/src/app.module.ts b/src/app.module.ts index ee3d951..33b9558 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,8 +1,10 @@ import { Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; import { HttpModule } from '@nestjs/axios'; import { PrometheusService } from './prometheus.service'; import { MetricsController } from './metrics.controller'; import { ConfigModule } from '@nestjs/config'; +import { AuthModule } from './auth/auth.module'; @Module({ imports: [ @@ -10,7 +12,20 @@ import { ConfigModule } from '@nestjs/config'; isGlobal: true, envFilePath: '.env', }), - HttpModule], + TypeOrmModule.forRoot({ + type: 'postgres', + host: process.env.DB_HOST, + port: parseInt(process.env.DB_PORT || '5432', 10), + username: process.env.DB_USER, + password: process.env.DB_PASSWORD, + database: process.env.DB_NAME, + entities: [__dirname + '/**/*.entity{.ts,.js}'], + synchronize: false, + logging: true, + }), + HttpModule, + AuthModule, + ], controllers: [MetricsController], providers: [PrometheusService], }) diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts new file mode 100644 index 0000000..bd328e3 --- /dev/null +++ b/src/auth/auth.controller.ts @@ -0,0 +1,16 @@ +import { Controller, Post, Body, UnauthorizedException } from '@nestjs/common'; +import { AuthService } from './auth.service'; + +@Controller('auth') +export class AuthController { + constructor(private authService: AuthService) { } + + @Post('login') + async login(@Body() body: { login: string; password: string }) { + const user = await this.authService.validateUser(body.login, body.password); + if (!user) { + throw new UnauthorizedException('Неверный логин или пароль'); + } + return { success: true, user }; + } +} diff --git a/src/auth/auth.module.ts b/src/auth/auth.module.ts new file mode 100644 index 0000000..8753795 --- /dev/null +++ b/src/auth/auth.module.ts @@ -0,0 +1,12 @@ +import { Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { AuthService } from './auth.service'; +import { AuthController } from './auth.controller'; +import { User } from './user.entity'; + +@Module({ + imports: [TypeOrmModule.forFeature([User])], + controllers: [AuthController], + providers: [AuthService], +}) +export class AuthModule { } diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts new file mode 100644 index 0000000..695c51c --- /dev/null +++ b/src/auth/auth.service.ts @@ -0,0 +1,31 @@ +import { Injectable } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; +import { Repository } from 'typeorm'; +import { User } from './user.entity'; + +@Injectable() +export class AuthService { + constructor( + @InjectRepository(User) + private usersRepository: Repository, + ) { } + + async validateUser(login: string, password: string): Promise { + console.log(`Проверка пользователя: ${login}, пароль: ${password}`); + + // Ищем пользователя по login + const user = await this.usersRepository.findOne({ where: { login } }); + + console.log(`Найденный пользователь:`, user); + + // Проверяем, что нашли пользователя и пароль совпадает + if (user && user.password === password) { + console.log(`Авторизация успешна`); + const { password, ...result } = user; + return result; + } + + console.log(`Ошибка: неверный логин или пароль`); + return null; + } +} diff --git a/src/auth/user.entity.ts b/src/auth/user.entity.ts new file mode 100644 index 0000000..fd88287 --- /dev/null +++ b/src/auth/user.entity.ts @@ -0,0 +1,13 @@ +import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'; + +@Entity('users') +export class User { + @PrimaryGeneratedColumn() + id: number; + + @Column() + login: string; + + @Column() + password: string; +}