mirror of
https://github.com/umami-software/umami.git
synced 2026-02-15 10:05:36 +01:00
Initial Typescript models.
This commit is contained in:
parent
04e9f06e93
commit
0aaba8cbd1
74 changed files with 1144 additions and 768 deletions
54
pages/api/users/[id]/password.ts
Normal file
54
pages/api/users/[id]/password.ts
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
import { getUser, updateUser } from 'queries';
|
||||
import { useAuth } from 'lib/middleware';
|
||||
import {
|
||||
badRequest,
|
||||
methodNotAllowed,
|
||||
ok,
|
||||
unauthorized,
|
||||
checkPassword,
|
||||
hashPassword,
|
||||
} from 'next-basics';
|
||||
import { allowQuery } from 'lib/auth';
|
||||
import { TYPE_USER } from 'lib/constants';
|
||||
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { User } from 'interface/api/models';
|
||||
|
||||
export interface UserPasswordRequestQuery {
|
||||
id: string;
|
||||
}
|
||||
|
||||
export interface UserPasswordRequestBody {
|
||||
current_password: string;
|
||||
new_password: string;
|
||||
}
|
||||
|
||||
export default async (
|
||||
req: NextApiRequestQueryBody<UserPasswordRequestQuery, UserPasswordRequestBody>,
|
||||
res: NextApiResponse<User>,
|
||||
) => {
|
||||
await useAuth(req, res);
|
||||
|
||||
const { current_password, new_password } = req.body;
|
||||
const { id } = req.query;
|
||||
|
||||
if (!(await allowQuery(req, TYPE_USER))) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
if (req.method === 'POST') {
|
||||
const user = await getUser({ id });
|
||||
|
||||
if (!checkPassword(current_password, user.password)) {
|
||||
return badRequest(res, 'Current password is incorrect');
|
||||
}
|
||||
|
||||
const password = hashPassword(new_password);
|
||||
|
||||
const updated = await updateUser({ password }, { id });
|
||||
|
||||
return ok(res, updated);
|
||||
}
|
||||
|
||||
return methodNotAllowed(res);
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue