35 lines
928 B
TypeScript
35 lines
928 B
TypeScript
import sqlite from "sqlite3";
|
|
import bcrypt from "bcryptjs";
|
|
|
|
const { CREDENTIALS_DB_PATH } = process.env;
|
|
if (!CREDENTIALS_DB_PATH) {
|
|
throw "$CREDENTIALS_DB_PATH not provided; unable to connect to database";
|
|
}
|
|
|
|
export const database = () => new sqlite.Database(CREDENTIALS_DB_PATH!);
|
|
|
|
export function validateCredentials(email: string, password: string) {
|
|
const db = database();
|
|
|
|
return new Promise<boolean>((resolve, reject) => {
|
|
db.get(
|
|
"SELECT key, value FROM passwords WHERE key = ?",
|
|
email,
|
|
async (err, row: any) => {
|
|
db.close(); // We don't need this anymore
|
|
|
|
if (err) return reject(err);
|
|
if (!row) return resolve(false);
|
|
|
|
try {
|
|
const hash: string = row.value.replace("bcrypt:", "");
|
|
const isValid = await bcrypt.compare(password, hash);
|
|
resolve(isValid);
|
|
} catch(e) {
|
|
reject(e);
|
|
}
|
|
}
|
|
);
|
|
});
|
|
}
|