@ -1,55 +1,10 @@
|
||||
import UserModel from "../../models/UserModel";
|
||||
import { connectMongo } from "./mongoHelpers";
|
||||
import { generateSalt, hashPassword } from "./passwordHelpers";
|
||||
import { randomString } from "./stringHelpers";
|
||||
|
||||
connectMongo();
|
||||
|
||||
/**
|
||||
* Returns the user with the given username
|
||||
* Returns the user with the given email address
|
||||
*
|
||||
* @param {string} username The users username
|
||||
* @return The user object in mongo or null if not found
|
||||
* @param {string} email The users email address
|
||||
* @return The users object in mongo or null if not found
|
||||
*/
|
||||
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),
|
||||
});
|
||||
user.save();
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
export async function getUser(email) {
|
||||
const user = await UserModel.find({ email: email });
|
||||
return user;
|
||||
}
|
||||
|
Reference in New Issue
Block a user