mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-16 02:05:04 +01:00
06bebadbb9
* Auth checkpoint. * Merge branch 'dev' into feat/um-114-roles-and-permissions
59 lines
1.3 KiB
TypeScript
59 lines
1.3 KiB
TypeScript
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
|
|
import { canCreateUser, canViewUsers } from 'lib/auth';
|
|
import { uuid } from 'lib/crypto';
|
|
import { useAuth } from 'lib/middleware';
|
|
import { NextApiResponse } from 'next';
|
|
import { badRequest, hashPassword, methodNotAllowed, ok, unauthorized } from 'next-basics';
|
|
import { createUser, getUser, getUsers, User } from 'queries';
|
|
|
|
export interface UsersRequestBody {
|
|
username: string;
|
|
password: string;
|
|
id: string;
|
|
}
|
|
|
|
export default async (
|
|
req: NextApiRequestQueryBody<any, UsersRequestBody>,
|
|
res: NextApiResponse<User[] | User>,
|
|
) => {
|
|
await useAuth(req, res);
|
|
|
|
const {
|
|
user: { id: userId },
|
|
} = req.auth;
|
|
|
|
if (req.method === 'GET') {
|
|
if (canViewUsers(userId)) {
|
|
return unauthorized(res);
|
|
}
|
|
|
|
const users = await getUsers();
|
|
|
|
return ok(res, users);
|
|
}
|
|
|
|
if (req.method === 'POST') {
|
|
if (canCreateUser(userId)) {
|
|
return unauthorized(res);
|
|
}
|
|
|
|
const { username, password, id } = req.body;
|
|
|
|
const user = await getUser({ username });
|
|
|
|
if (user) {
|
|
return badRequest(res, 'User already exists');
|
|
}
|
|
|
|
const created = await createUser({
|
|
id: id || uuid(),
|
|
username,
|
|
password: hashPassword(password),
|
|
});
|
|
|
|
return ok(res, created);
|
|
}
|
|
|
|
return methodNotAllowed(res);
|
|
};
|