Controlador
This commit is contained in:
Generated
+1
@@ -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"
|
||||
|
||||
+2
-2
@@ -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"
|
||||
},
|
||||
|
||||
+2
-2
@@ -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,
|
||||
}),
|
||||
}),
|
||||
],
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
import { IsNotEmpty } from "class-validator";
|
||||
|
||||
export class registerDto{
|
||||
@IsNotEmpty()
|
||||
username: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
password: string;
|
||||
|
||||
|
||||
}
|
||||
@@ -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<string, any>) {
|
||||
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<string, any>) {
|
||||
return this.authService.create(createUserDto.username,createUserDto.password);
|
||||
|
||||
@@ -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],
|
||||
|
||||
+42
-11
@@ -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<User>
|
||||
) {}
|
||||
|
||||
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(
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -14,9 +14,10 @@ export class UsersService {
|
||||
return this.users.findOne({ where: { username } });
|
||||
}
|
||||
|
||||
async create(user: User): Promise<User> {
|
||||
async create(user:User): Promise<User> {
|
||||
return this.users.save(user);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user