bcrypt module y service final

This commit is contained in:
xXpuma99Xx
2022-12-19 11:39:12 -06:00
parent f893493b80
commit f899648f42
2 changed files with 17 additions and 15 deletions
+1 -4
View File
@@ -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 {}
+16 -11
View File
@@ -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;
};