@a11ywatch/core
Version:
a11ywatch central API
110 lines • 5.88 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createUser = void 0;
const config_1 = require("../../../../config");
const models_1 = require("../../../../core/models");
const strings_1 = require("../../../strings");
const utils_1 = require("../../../utils");
const counters_1 = require("../../counters");
const find_1 = require("../find");
const confirm_email_1 = require("../update/confirm-email");
const createUser = ({ email, password, googleId, githubId: ghID, role: roleID = 0, }) => __awaiter(void 0, void 0, void 0, function* () {
if (!email) {
throw new Error(strings_1.EMAIL_ERROR);
}
const [user, collection] = yield (0, find_1.getUser)({ email });
const role = config_1.SUPER_MODE && !roleID ? 1 : roleID;
const githubId = typeof ghID !== "undefined" ? Number(ghID) : null;
const googleAuthed = user && (user.googleId || googleId);
const githubAuthed = user && (user.githubId || githubId);
if (!googleId && !githubId && !password) {
throw new Error("Password of atleast 6 chars required to register.");
}
const salthash = password && (yield (0, utils_1.saltHashPassword)(password, user === null || user === void 0 ? void 0 : user.salt));
const passwordMatch = (user === null || user === void 0 ? void 0 : user.password) === (salthash === null || salthash === void 0 ? void 0 : salthash.passwordHash);
const shouldValidatePassword = (user === null || user === void 0 ? void 0 : user.password) && !googleId && !githubId;
if (shouldValidatePassword && passwordMatch === false) {
throw new Error("Account already exist. Please check your email and try again.");
}
const emailConfirmed = !!googleId || !!githubId;
if (user) {
user.emailConfirmed = emailConfirmed;
if (!googleId && !githubId && password && !(user === null || user === void 0 ? void 0 : user.password)) {
throw new Error(user.googleId || user.githubId
? "Password not found, try using your google login or reset the password."
: "Account reset password required, please reset the password by going to https://a11ywatch.com/reset-password to continue.");
}
if (googleId && (user === null || user === void 0 ? void 0 : user.googleId) && (user === null || user === void 0 ? void 0 : user.googleId) !== googleId) {
throw new Error("GoogleID is not tied to any user.");
}
if (!googleId &&
typeof githubId !== "undefined" &&
(user === null || user === void 0 ? void 0 : user.githubId) &&
(user === null || user === void 0 ? void 0 : user.githubId) !== githubId) {
throw new Error("GithubId is not tied to any user.");
}
}
if ((user && (user === null || user === void 0 ? void 0 : user.salt)) || googleAuthed || githubAuthed) {
if (passwordMatch || googleId || githubId) {
let keyid = user === null || user === void 0 ? void 0 : user.id;
let updateCollectionProps = {};
if (typeof (user === null || user === void 0 ? void 0 : user.id) === "undefined" || (user === null || user === void 0 ? void 0 : user.id) === null) {
keyid = yield (0, counters_1.getNextSequenceValue)("Users");
updateCollectionProps = { id: keyid };
}
const jwt = (0, utils_1.signJwt)({
email: (user === null || user === void 0 ? void 0 : user.email) || email,
role: (user === null || user === void 0 ? void 0 : user.role) || role || 0,
keyid,
});
updateCollectionProps = Object.assign(Object.assign({}, updateCollectionProps), { jwt, lastLoginDate: new Date() });
if (googleId) {
updateCollectionProps = Object.assign(Object.assign({}, updateCollectionProps), { googleId,
emailConfirmed });
}
if (githubId) {
updateCollectionProps = Object.assign(Object.assign({}, updateCollectionProps), { githubId,
emailConfirmed });
}
yield collection.updateOne({ email }, {
$set: updateCollectionProps,
}, {
upsert: true,
});
return user;
}
else {
throw new Error(strings_1.EMAIL_ERROR);
}
}
else {
const id = yield (0, counters_1.getNextSequenceValue)("Users");
const userObject = (0, models_1.makeUser)({
email,
password: salthash === null || salthash === void 0 ? void 0 : salthash.passwordHash,
salt: salthash === null || salthash === void 0 ? void 0 : salthash.salt,
id,
jwt: (0, utils_1.signJwt)({ email, role, keyid: id }),
role,
googleId,
githubId,
emailConfirmed,
});
yield collection.insertOne(userObject);
if (!emailConfirmed) {
yield (0, confirm_email_1.confirmEmail)({ keyid: id });
}
return userObject;
}
});
exports.createUser = createUser;
//# sourceMappingURL=create.js.map
;