EP register and sign in finish
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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"},
|
||||
}
|
||||
}
|
||||
}),
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
@@ -15,9 +15,15 @@ export class UsersService {
|
||||
}
|
||||
|
||||
async create(user:User): Promise<User> {
|
||||
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<void> {
|
||||
await this.users.update(id, { jwt, lastlog, expirationjwt });
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user