added protection against duplicate user docs

This commit is contained in:
Jack 2021-09-13 15:40:00 +01:00
parent 3d57ad2ec9
commit 7e682136f7

View file

@ -6,6 +6,9 @@ const { updateAuthEmail } = require("../handlers/auth");
class UsersDAO {
static async addUser(name, email, uid) {
const user = await mongoDB().collection("users").findOne({ uid });
if (user)
throw new MonkeyError(400, "User document already exists", "addUser");
return await mongoDB()
.collection("users")
.insertOne({ name, email, uid, addedAt: Date.now() });
@ -64,7 +67,7 @@ class UsersDAO {
static async getUser(uid) {
const user = await mongoDB().collection("users").findOne({ uid });
if (!user) throw new MonkeyError(404, "User not found", "get user");
if (user) throw new MonkeyError(404, "User not found", "get user");
return user;
}