Skip to content

Commit

Permalink
Change to synchronous password hashing.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecao committed May 24, 2021
1 parent 756beb2 commit b2d04c0
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ export function getRandomChars(n) {
return s;
}

export async function hashPassword(password) {
export function hashPassword(password) {
return bcrypt.hashSync(password, SALT_ROUNDS);
}

export async function checkPassword(password, hash) {
export function checkPassword(password, hash) {
return bcrypt.compareSync(password, hash);
}

Expand Down
4 changes: 2 additions & 2 deletions pages/api/account/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default async (req, res) => {
const data = {};

if (password) {
data.password = await hashPassword(password);
data.password = hashPassword(password);
}

// Only admin can change these fields
Expand Down Expand Up @@ -51,7 +51,7 @@ export default async (req, res) => {
return badRequest(res, 'Account already exists');
}

const created = await createAccount({ username, password: await hashPassword(password) });
const created = await createAccount({ username, password: hashPassword(password) });

return ok(res, created);
}
Expand Down
4 changes: 2 additions & 2 deletions pages/api/account/password.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ export default async (req, res) => {

if (req.method === 'POST') {
const account = await getAccountById(user_id);
const valid = await checkPassword(current_password, account.password);
const valid = checkPassword(current_password, account.password);

if (!valid) {
return badRequest(res, 'Current password is incorrect');
}

const password = await hashPassword(new_password);
const password = hashPassword(new_password);

const updated = await updateAccount(user_id, { password });

Expand Down
6 changes: 3 additions & 3 deletions prisma/seed.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const bcrypt = require('bcrypt');
const bcrypt = require('bcryptjs');
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
const SALT_ROUNDS = 10;

const hashPassword = password => {
return bcrypt.hash(password, SALT_ROUNDS);
return bcrypt.hashSync(password, SALT_ROUNDS);
};

async function main() {
const password = await hashPassword(process.env.ADMIN_PASSWORD || 'umami');
const password = hashPassword(process.env.ADMIN_PASSWORD || 'umami');
await prisma.account.upsert({
where: { username: 'admin' },
update: {},
Expand Down
6 changes: 3 additions & 3 deletions scripts/change-password.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require('dotenv').config();
const bcrypt = require('bcrypt');
const bcrypt = require('bcryptjs');
const chalk = require('chalk');
const prompts = require('prompts');
const { PrismaClient } = require('@prisma/client');
Expand All @@ -25,11 +25,11 @@ const updateAccountByUsername = (username, data) => {
};

const hashPassword = password => {
return bcrypt.hash(password, SALT_ROUNDS);
return bcrypt.hashSync(password, SALT_ROUNDS);
};

const changePassword = async (username, newPassword) => {
const password = await hashPassword(newPassword);
const password = hashPassword(newPassword);
return updateAccountByUsername(username, { password });
};

Expand Down

0 comments on commit b2d04c0

Please sign in to comment.