Add hashing y verificación de contraseña
This commit is contained in:
11
mvp/b2c/src/lib/auth/password.ts
Normal file
11
mvp/b2c/src/lib/auth/password.ts
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
import bcrypt from 'bcryptjs';
|
||||||
|
|
||||||
|
const SALT_ROUNDS = 10;
|
||||||
|
|
||||||
|
export function hashPassword(plain: string): Promise<string> {
|
||||||
|
return bcrypt.hash(plain, SALT_ROUNDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function verifyPassword(plain: string, hash: string): Promise<boolean> {
|
||||||
|
return bcrypt.compare(plain, hash);
|
||||||
|
}
|
||||||
15
mvp/b2c/tests/auth/password.test.ts
Normal file
15
mvp/b2c/tests/auth/password.test.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import { describe, it, expect } from 'vitest';
|
||||||
|
import { hashPassword, verifyPassword } from '@/lib/auth/password';
|
||||||
|
|
||||||
|
describe('password', () => {
|
||||||
|
it('verifica una contraseña correcta contra su hash', async () => {
|
||||||
|
const hash = await hashPassword('Reforma2026!');
|
||||||
|
expect(hash).not.toBe('Reforma2026!');
|
||||||
|
expect(await verifyPassword('Reforma2026!', hash)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rechaza una contraseña incorrecta', async () => {
|
||||||
|
const hash = await hashPassword('Reforma2026!');
|
||||||
|
expect(await verifyPassword('otra', hash)).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user