mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-16 02:05:04 +01:00
41 lines
873 B
JavaScript
41 lines
873 B
JavaScript
import { ok, unauthorized, methodNotAllowed, badRequest, hashPassword } from 'next-basics';
|
|
import { useAuth } from 'lib/middleware';
|
|
import { uuid } from 'lib/crypto';
|
|
import { createUser, getUser, getUsers } from 'queries';
|
|
|
|
export default async (req, res) => {
|
|
await useAuth(req, res);
|
|
|
|
const { isAdmin } = req.auth.user;
|
|
|
|
if (!isAdmin) {
|
|
return unauthorized(res);
|
|
}
|
|
|
|
if (req.method === 'GET') {
|
|
const users = await getUsers();
|
|
|
|
return ok(res, users);
|
|
}
|
|
|
|
if (req.method === 'POST') {
|
|
const { username, password } = req.body;
|
|
|
|
const user = await getUser({ username });
|
|
|
|
if (user) {
|
|
return badRequest(res, 'User already exists');
|
|
}
|
|
|
|
const created = await createUser({
|
|
id: uuid(),
|
|
username,
|
|
password: hashPassword(password),
|
|
});
|
|
|
|
return ok(res, created);
|
|
}
|
|
|
|
return methodNotAllowed(res);
|
|
};
|