feat: add salting to password

This commit is contained in:
bekacru
2024-05-20 22:53:24 +03:00
parent d4a620fb89
commit 2c089d7969
2 changed files with 11 additions and 4 deletions

View File

@@ -1,11 +1,18 @@
import * as argon2 from "argon2";
import { generateRandomString } from "./random";
export const hashPassword = async (password: string) => {
return argon2.hash(password, {
export const hashPassword = async (password: string, secret: string) => {
const salt = generateRandomString(12);
const hash = await argon2.hash(password, {
type: argon2.argon2id,
salt,
secret,
});
return `${hash}$${salt}`;
};
export const validatePassword = async (password: string, hash: string) => {
return argon2.verify(hash, password);
const [hashPart, salt] = hash.split("$");
if (!hashPart || !salt) return false;
return argon2.verify(hashPart, password);
};

View File

@@ -93,7 +93,7 @@ export const credential = <O extends CredentialOption>(options?: O) => {
{
user: {
...data,
["password"]: await hashPassword(data["password"]),
["password"]: await hashPassword(data["password"], context.secret),
emailVerified: false,
},
account: {