From e61268a66a5054dd8fb09c34ee24ea6da9d82996 Mon Sep 17 00:00:00 2001 From: IO Date: Tue, 11 Jun 2024 19:58:36 -0600 Subject: [PATCH] EP register and sign in finish --- src/auth/Dto/registerDto.dto.ts | 5 ++++- src/auth/auth.controller.ts | 2 +- src/auth/auth.module.ts | 4 +--- src/auth/auth.service.ts | 23 ++++++++++++++--------- src/users/dto/create-user.dto.ts | 7 ------- src/users/users.service.ts | 8 +++++++- 6 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/auth/Dto/registerDto.dto.ts b/src/auth/Dto/registerDto.dto.ts index 57e790f..54b1c78 100644 --- a/src/auth/Dto/registerDto.dto.ts +++ b/src/auth/Dto/registerDto.dto.ts @@ -1,9 +1,12 @@ -import { IsNotEmpty } from "class-validator"; +import {IsString, IsNotEmpty } from "class-validator"; export class registerDto{ + + @IsString() @IsNotEmpty() username: string; + @IsString() @IsNotEmpty() password: string; diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index 0414686..a319600 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -13,7 +13,7 @@ import { } from '@nestjs/common'; import { AuthGuard } from './auth.guard'; import { AuthService } from './auth.service'; -import { registerDto } from './Dto/registerDto.dto'; +import { registerDto } from './dto/registerDto.dto'; @Controller('auth') export class AuthController { diff --git a/src/auth/auth.module.ts b/src/auth/auth.module.ts index ac7408a..5910219 100644 --- a/src/auth/auth.module.ts +++ b/src/auth/auth.module.ts @@ -18,9 +18,7 @@ import { User } from 'src/users/entities/user.entity'; return{ global: true, secret: configService.get('JWT_SECRET'), - signOptions: { - expiresIn: configService.get('JWT_EXPIRE') - }, + signOptions: { expiresIn: "1h"}, } } }), diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index 095d09c..e23b824 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -2,15 +2,13 @@ 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 { 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( @@ -21,26 +19,30 @@ export class AuthService { async register(data: registerDto){ const{username, password,} = data; - if(await this.userRepository.findOne({where:{username:data.username}})){ + + //Seeks the password and if it finds it, it doesn't register + if(await this.usersService.findOne(data.username)){ throw new HttpException("User already exist",403); } + //Encrypt the password const hashedpassword = await hash(password, 10); + //Place the password in data data = {...data, password:hashedpassword} - return this.userRepository.save( this.userRepository.create(data) ) } async signIn(data: registerDto) { - - const{ password} = data; + const{password} = data; const user = await this.usersService.findOne(data.username); + //compare encrypt password with the password entered const checkPassword = await compare(password, (await user).password) + //if the password is incorrect, won't let you in if (!checkPassword) { throw new UnauthorizedException(); } @@ -48,11 +50,14 @@ export class AuthService { idUser: user.userId, username: user.username }; + + //create the token with the payload const token = this.jwtService.sign(payload); - data = {...data},token; + //update all + await this.usersService.updateTokenAndDates(user.userId,token, new Date(), new Date(Date.now() + 3600 * 1000)); - return data; + return {token: token}; } async create( diff --git a/src/users/dto/create-user.dto.ts b/src/users/dto/create-user.dto.ts index 17a7a0c..05d95e8 100644 --- a/src/users/dto/create-user.dto.ts +++ b/src/users/dto/create-user.dto.ts @@ -4,15 +4,8 @@ export class CreateUserDto{ @IsString() readonly name: string; - @IsNotEmpty() - @IsEmail() - readonly email: string; - @IsNotEmpty() @MinLength(10) readonly password: string; - @IsString() - readonly phone: string; - } \ No newline at end of file diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 68918a9..af7a994 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -15,9 +15,15 @@ export class UsersService { } async create(user:User): Promise { - return this.users.save(user); + const newUser = this.users.create(user); + return this.users.save(newUser); } + async updateTokenAndDates(id: number, jwt: string, lastlog: Date, expirationjwt: Date): Promise { + await this.users.update(id, { jwt, lastlog, expirationjwt }); + } + + }