feat: add otp and hashing modules
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { HashingProvider } from './providers/hashing.provider';
|
||||
import { BcryptProvider } from './providers/bcrypt.provider';
|
||||
|
||||
@Module({
|
||||
providers: [
|
||||
{
|
||||
provide: HashingProvider,
|
||||
useClass: BcryptProvider,
|
||||
},
|
||||
],
|
||||
exports: [HashingProvider],
|
||||
})
|
||||
export class HashingModule {}
|
||||
@@ -0,0 +1,22 @@
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { HashingProvider } from './hashing.provider';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
|
||||
@Injectable()
|
||||
export class BcryptProvider implements HashingProvider {
|
||||
public async hash(data: string | Buffer): Promise<string> {
|
||||
const salt = (await bcrypt.genSalt()) as string;
|
||||
|
||||
return (await bcrypt.hash(data, salt)) as string;
|
||||
}
|
||||
|
||||
public async compare(
|
||||
data: string | Buffer,
|
||||
encrypted: string,
|
||||
): Promise<boolean> {
|
||||
return await bcrypt.compare(data, encrypted);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export abstract class HashingProvider {
|
||||
abstract hash(data: string | Buffer): Promise<string>;
|
||||
|
||||
abstract compare(data: string | Buffer, encrypted: string): Promise<boolean>;
|
||||
}
|
||||
Reference in New Issue
Block a user