bcrypt module y service final
This commit is contained in:
@@ -1,8 +1,5 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { BcryptService } from './bcrypt.service';
|
||||
|
||||
@Module({
|
||||
providers: [BcryptService],
|
||||
exports: [BcryptService],
|
||||
})
|
||||
@Module({ providers: [BcryptService], exports: [BcryptService] })
|
||||
export class BcryptModule {}
|
||||
|
||||
@@ -4,27 +4,32 @@ import { ConfigService } from '@nestjs/config';
|
||||
|
||||
@Injectable()
|
||||
export class BcryptService {
|
||||
constructor(private configService: ConfigService) {}
|
||||
private salt: string;
|
||||
private passwordLength: number;
|
||||
|
||||
comparar(password: string, passwordDb: string) {
|
||||
constructor(private configService: ConfigService) {
|
||||
this.salt = bcrypt.genSaltSync(
|
||||
parseInt(this.configService.get<string>('SALT_ROUNDS')),
|
||||
);
|
||||
this.passwordLength = parseInt(
|
||||
this.configService.get<string>('PASSWORD_LENGTH'),
|
||||
);
|
||||
}
|
||||
|
||||
comparar(password: string, passwordDb: string): boolean {
|
||||
return bcrypt.compareSync(password, passwordDb);
|
||||
}
|
||||
|
||||
encriptar(password: string) {
|
||||
const salt = bcrypt.genSaltSync(
|
||||
parseInt(this.configService.get<string>('SALT_ROUNDS')),
|
||||
);
|
||||
|
||||
return bcrypt.hashSync(password, salt);
|
||||
encriptar(password: string): string {
|
||||
return bcrypt.hashSync(password, this.salt);
|
||||
}
|
||||
|
||||
generarPassword = () => {
|
||||
const length = 10;
|
||||
generarPassword = (): string => {
|
||||
const charset =
|
||||
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-@#$%&=?¿*()/_-';
|
||||
let password = '';
|
||||
|
||||
for (let i = 0; i < length; i++)
|
||||
for (let i = 0; i < this.passwordLength; i++)
|
||||
password += charset.charAt(Math.floor(Math.random() * charset.length));
|
||||
return password;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user