This repository has been archived on 2023-10-27. You can view files and clone it, but cannot push or open issues or pull requests.
Files
imageify/src/utils/helpers/userHelpers.js

63 lines
1.5 KiB
JavaScript
Raw Normal View History

import UserModel from "../../models/UserModel";
import { connectMongo } from "./mongoHelpers";
import { generateSalt, hashPassword } from "./passwordHelpers";
import { randomString } from "./stringHelpers";
connectMongo();
2022-11-13 20:19:41 +00:00
/**
* Returns the user with the given username
2022-11-13 20:19:41 +00:00
*
* @param {string} username The users username
* @return The user object in mongo or null if not found
2022-11-13 20:19:41 +00:00
*/
export async function getUser(username) {
return await UserModel.findOne({ username: username });
}
/**
* Returns the user with the given upload key
*
* @param {string} uploadKey The users uploadKey
* @return The user object in mongo or null if not found
*/
export async function getUserByUploadKey(uploadKey) {
return await UserModel.findOne({ uploadKey: uploadKey });
}
/**
* Creates a new user and returns the user object
*
* @param {string} username The username of the account
* @param {string} password The non hashed password of the account
*
* @return null if user already exists, true if success, false if fail
*/
export async function createUser(username, password) {
let user = await getUser(username);
if (user !== null) {
return null;
}
try {
const salt = generateSalt();
user = await UserModel.create({
username: username,
password: hashPassword(salt, password),
salt: salt,
uploadKey: randomString(16),
discordEmbed: {
embed: true,
title: "{id}.{ext}",
description: null,
color: "0063EB",
},
});
user.save();
} catch (e) {
return false;
}
return true;
2022-11-13 20:19:41 +00:00
}