Fixed change password issue. API refactoring. Closes #1592.

This commit is contained in:
Mike Cao 2022-10-25 10:45:56 -07:00
parent 994cf950c8
commit ac070d3ce9
24 changed files with 74 additions and 111 deletions

View file

@ -1,4 +1,4 @@
import { getAccountById, updateAccount } from 'queries';
import { getAccount, updateAccount } from 'queries';
import { useAuth } from 'lib/middleware';
import {
badRequest,
@ -8,21 +8,21 @@ import {
checkPassword,
hashPassword,
} from 'next-basics';
import { allowQuery } from 'lib/auth';
import { TYPE_ACCOUNT } from 'lib/constants';
export default async (req, res) => {
await useAuth(req, res);
const { userId: currentUserId, isAdmin: currentUserIsAdmin } = req.auth;
const { current_password, new_password } = req.body;
const { id } = req.query;
const userId = +id;
const { id: accountUuid } = req.query;
if (!currentUserIsAdmin && userId !== currentUserId) {
if (!(await allowQuery(req, TYPE_ACCOUNT))) {
return unauthorized(res);
}
if (req.method === 'POST') {
const account = await getAccountById(userId);
const account = await getAccount({ accountUuid });
if (!checkPassword(current_password, account.password)) {
return badRequest(res, 'Current password is incorrect');
@ -30,7 +30,7 @@ export default async (req, res) => {
const password = hashPassword(new_password);
const updated = await updateAccount(userId, { password });
const updated = await updateAccount({ password }, { accountUuid });
return ok(res, updated);
}

View file

@ -1,7 +1,7 @@
import { ok, unauthorized, methodNotAllowed, badRequest, hashPassword } from 'next-basics';
import { useAuth } from 'lib/middleware';
import { uuid } from 'lib/crypto';
import { createAccount, getAccountByUsername, getAccounts } from 'queries';
import { createAccount, getAccount, getAccounts } from 'queries';
export default async (req, res) => {
await useAuth(req, res);
@ -21,7 +21,7 @@ export default async (req, res) => {
if (req.method === 'POST') {
const { username, password, account_uuid } = req.body;
const accountByUsername = await getAccountByUsername(username);
const accountByUsername = await getAccount({ username });
if (accountByUsername) {
return badRequest(res, 'Account already exists');