diff --git a/package-lock.json b/package-lock.json index 5c0980f..2535288 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3264,6 +3264,7 @@ "resolved": "https://registry.npmjs.org/bcrypt/-/bcrypt-5.1.1.tgz", "integrity": "sha512-AGBHOG5hPYZ5Xl9KXzU5iKq9516yEmvCKDg3ecP5kX2aB6UqTeXZxk2ELnDgDm6BQSMlLt9rDB4LoSMx0rYwww==", "hasInstallScript": true, + "license": "MIT", "dependencies": { "@mapbox/node-pre-gyp": "^1.0.11", "node-addon-api": "^5.0.0" diff --git a/package.json b/package.json index 62bdda5..ad1aa40 100644 --- a/package.json +++ b/package.json @@ -24,17 +24,17 @@ "@nestjs/config": "^3.2.2", "@nestjs/core": "^10.0.0", "@nestjs/jwt": "^10.2.0", - "@nestjs/passport": "^10.0.3", "@nestjs/mapped-types": "*", + "@nestjs/passport": "^10.0.3", "@nestjs/platform-express": "^10.0.0", "@nestjs/typeorm": "^10.0.2", + "bcrypt": "^5.1.1", "bcryptjs": "^2.4.3", "class-transformer": "^0.5.1", "class-validator": "^0.14.1", "mysql2": "^3.10.0", "passport": "^0.7.0", "passport-jwt": "^4.0.1", - "bcrypt": "^5.1.1", "reflect-metadata": "^0.2.0", "rxjs": "^7.8.1" }, diff --git a/src/app.module.ts b/src/app.module.ts index a8bd01b..0626d93 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -16,7 +16,7 @@ import { User } from "./users/entities/user.entity"; isGlobal: true, }), TypeOrmModule.forRootAsync({ - imports: [ConfigModule], + imports: [ConfigModule,AuthModule,UsersModule,], inject: [ConfigService], useFactory: async (configservice: ConfigService) => ({ type: 'mariadb', @@ -26,7 +26,7 @@ import { User } from "./users/entities/user.entity"; password: configservice.get('DB_PASSWORD'), database: configservice.get('DB_DATABASE'), entities: [User], - synchronize: configservice.get('DB_SYNCRONIZE'), + synchronize:true, }), }), ], diff --git a/src/auth/Dto/registerDto.dto.ts b/src/auth/Dto/registerDto.dto.ts new file mode 100644 index 0000000..57e790f --- /dev/null +++ b/src/auth/Dto/registerDto.dto.ts @@ -0,0 +1,11 @@ +import { IsNotEmpty } from "class-validator"; + +export class registerDto{ + @IsNotEmpty() + username: string; + + @IsNotEmpty() + password: string; + + +} \ No newline at end of file diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index 29387ee..0414686 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -13,6 +13,7 @@ import { } from '@nestjs/common'; import { AuthGuard } from './auth.guard'; import { AuthService } from './auth.service'; +import { registerDto } from './Dto/registerDto.dto'; @Controller('auth') export class AuthController { @@ -20,8 +21,8 @@ export class AuthController { @HttpCode(HttpStatus.OK) @Post('login') - signIn(@Body() signInDto: Record) { - return this.authService.signIn(signInDto.username, signInDto.password); + signIn(@Body() data: registerDto) { + return this.authService.signIn(data); } @UseGuards(AuthGuard) @@ -30,6 +31,11 @@ export class AuthController { return req.user; } + @Post("register") + postRegister(@Body() data: registerDto){ + return this.authService.register(data) + } + @Post('Alta') async create(@Body() createUserDto: Record) { return this.authService.create(createUserDto.username,createUserDto.password); diff --git a/src/auth/auth.module.ts b/src/auth/auth.module.ts index b4426e9..ac7408a 100644 --- a/src/auth/auth.module.ts +++ b/src/auth/auth.module.ts @@ -5,6 +5,9 @@ import { UsersModule } from '../users/users.module'; import { JwtModule } from '@nestjs/jwt'; import { AuthController } from './auth.controller'; import { ConfigService } from '@nestjs/config'; +import { UsersService } from 'src/users/users.service'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { User } from 'src/users/entities/user.entity'; @Module({ imports: [ @@ -21,6 +24,7 @@ import { ConfigService } from '@nestjs/config'; } } }), + TypeOrmModule.forFeature([User]) ], providers: [AuthService], controllers: [AuthController], diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index 1196dcd..095d09c 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -1,27 +1,58 @@ -import { Injectable, UnauthorizedException } from '@nestjs/common'; +import { HttpException, Inject, Injectable, UnauthorizedException } from '@nestjs/common'; import { UsersService } from '../users/users.service'; import { JwtService } from '@nestjs/jwt'; +import { registerDto } from './Dto/registerDto.dto'; +import { User } from 'src/users/entities/user.entity'; +import { InjectRepository } from '@nestjs/typeorm'; +import { Repository } from 'typeorm'; +import *as bcrypt from 'bcrypt'; +import { hash } from 'bcrypt'; +import { compare } from 'bcryptjs'; + @Injectable() export class AuthService { constructor( private usersService: UsersService, - private jwtService: JwtService + private jwtService: JwtService, + @InjectRepository(User) private userRepository: Repository ) {} - async signIn( - username: string, - pass: string, - ): Promise<{ access_token: string }> { - const user = await this.usersService.findOne(username); - if (user?.password !== pass) { + async register(data: registerDto){ + const{username, password,} = data; + if(await this.userRepository.findOne({where:{username:data.username}})){ + throw new HttpException("User already exist",403); + } + + const hashedpassword = await hash(password, 10); + + data = {...data, password:hashedpassword} + + return this.userRepository.save( + this.userRepository.create(data) + ) + } + + async signIn(data: registerDto) { + + const{ password} = data; + const user = await this.usersService.findOne(data.username); + + const checkPassword = await compare(password, (await user).password) + + if (!checkPassword) { throw new UnauthorizedException(); } - const payload = { sub: user.userId, username: user.username }; - return { - access_token: await this.jwtService.signAsync(payload), + const payload = { + idUser: user.userId, + username: user.username }; + const token = this.jwtService.sign(payload); + + data = {...data},token; + + return data; } async create( diff --git a/src/users/entities/user.entity.ts b/src/users/entities/user.entity.ts index 8e6da9b..192ed97 100644 --- a/src/users/entities/user.entity.ts +++ b/src/users/entities/user.entity.ts @@ -11,8 +11,14 @@ export class User { @Column() password: string; - @Column() + @Column({nullable:true}) jwt : string; + @Column({nullable:true}) + expirationjwt: Date; + + @Column({nullable:true}) + lastlog: Date; + } diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 5fc5069..68918a9 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -14,9 +14,10 @@ export class UsersService { return this.users.findOne({ where: { username } }); } - async create(user: User): Promise { + async create(user:User): Promise { return this.users.save(user); } + }