@a11ywatch/core
Version:
a11ywatch central API
70 lines • 3.74 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.verifyUser = void 0;
const strings_1 = require("../../../strings");
const utils_1 = require("../../../utils");
const counters_1 = require("../../counters");
const find_1 = require("../find");
const verifyUser = ({ password, email, googleId, githubId, }) => __awaiter(void 0, void 0, void 0, function* () {
if (!email) {
throw new Error(strings_1.EMAIL_ERROR);
}
if (!password && !googleId && !githubId) {
throw new Error("A password is required to login.");
}
const [user, collection] = yield (0, find_1.getUser)({ email });
if (!user) {
throw new Error(strings_1.EMAIL_ERROR);
}
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 = !passwordMatch && !googleId && !githubId;
if (shouldValidatePassword) {
throw new Error(strings_1.EMAIL_ERROR);
}
const googleLoginAttempt = typeof googleId !== "undefined";
const githubLoginAttempt = typeof githubId !== "undefined";
if (googleLoginAttempt) {
const isGoogleMatch = (user === null || user === void 0 ? void 0 : user.googleId) == googleId || !(user === null || user === void 0 ? void 0 : user.googleId);
if (!isGoogleMatch) {
throw new Error("Google ID is not tied to user.");
}
}
if (githubLoginAttempt) {
const isGithubMatch = (user === null || user === void 0 ? void 0 : user.githubId) == githubId || !(user === null || user === void 0 ? void 0 : user.githubId);
if (!isGithubMatch) {
throw new Error("Github ID is not tied to user.");
}
}
let id = user === null || user === void 0 ? void 0 : user.id;
let updateCollectionProps = {};
if ((user === null || user === void 0 ? void 0 : user.id) === null) {
id = yield (0, counters_1.getNextSequenceValue)("Users");
updateCollectionProps = { id };
}
const jwt = (0, utils_1.signJwt)({
email: email || (user === null || user === void 0 ? void 0 : user.email),
role: user === null || user === void 0 ? void 0 : user.role,
keyid: id,
});
updateCollectionProps = Object.assign(Object.assign({}, updateCollectionProps), { jwt, lastLoginDate: new Date() });
if (googleLoginAttempt) {
updateCollectionProps = Object.assign(Object.assign({}, updateCollectionProps), { googleId });
}
if (githubLoginAttempt) {
updateCollectionProps = Object.assign(Object.assign({}, updateCollectionProps), { githubId });
}
yield collection.updateOne({ email }, { $set: updateCollectionProps }, { upsert: true });
return Object.assign(Object.assign({}, user), { jwt });
});
exports.verifyUser = verifyUser;
//# sourceMappingURL=verify.js.map
;