160 lines
6.6 KiB
JavaScript
160 lines
6.6 KiB
JavaScript
"use strict";
|
|
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());
|
|
});
|
|
};
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.default = apiLoginUser;
|
|
const grab_db_full_name_1 = __importDefault(require("../../../utils/grab-db-full-name"));
|
|
const varDatabaseDbHandler_1 = __importDefault(require("../../backend/varDatabaseDbHandler"));
|
|
const hashPassword_1 = __importDefault(require("../../dsql/hashPassword"));
|
|
/**
|
|
* # API Login
|
|
*/
|
|
function apiLoginUser(_a) {
|
|
return __awaiter(this, arguments, void 0, function* ({ encryptionKey, email, username, password, database, additionalFields, email_login, email_login_code, email_login_field, token, skipPassword, social, useLocal, dbUserId, debug, }) {
|
|
const dbFullName = (0, grab_db_full_name_1.default)({ dbName: database, userId: dbUserId });
|
|
/**
|
|
* Check input validity
|
|
*
|
|
* @description Check input validity
|
|
*/
|
|
if ((email === null || email === void 0 ? void 0 : email.match(/ /)) ||
|
|
(username && (username === null || username === void 0 ? void 0 : username.match(/ /))) ||
|
|
(password && (password === null || password === void 0 ? void 0 : password.match(/ /)))) {
|
|
return {
|
|
success: false,
|
|
msg: "Invalid Email/Password format",
|
|
};
|
|
}
|
|
/**
|
|
* Password hash
|
|
*
|
|
* @description Password hash
|
|
*/
|
|
let hashedPassword = password
|
|
? (0, hashPassword_1.default)({
|
|
encryptionKey: encryptionKey,
|
|
password: password,
|
|
})
|
|
: null;
|
|
if (debug) {
|
|
console.log("apiLoginUser:database:", dbFullName);
|
|
console.log("apiLoginUser:Finding User ...");
|
|
}
|
|
let foundUser = yield (0, varDatabaseDbHandler_1.default)({
|
|
queryString: `SELECT * FROM ${dbFullName}.users WHERE email = ? OR username = ?`,
|
|
queryValuesArray: [email, username],
|
|
database: dbFullName,
|
|
useLocal,
|
|
debug,
|
|
});
|
|
if (debug) {
|
|
console.log("apiLoginUser:foundUser:", foundUser);
|
|
}
|
|
if ((!foundUser || !foundUser[0]) && !social)
|
|
return {
|
|
success: false,
|
|
payload: null,
|
|
msg: "No user found",
|
|
};
|
|
let isPasswordCorrect = false;
|
|
if (debug) {
|
|
console.log("apiLoginUser:isPasswordCorrect:", isPasswordCorrect);
|
|
}
|
|
if ((foundUser === null || foundUser === void 0 ? void 0 : foundUser[0]) && !email_login && skipPassword) {
|
|
isPasswordCorrect = true;
|
|
}
|
|
else if ((foundUser === null || foundUser === void 0 ? void 0 : foundUser[0]) && !email_login) {
|
|
isPasswordCorrect = hashedPassword === foundUser[0].password;
|
|
}
|
|
else if (foundUser &&
|
|
foundUser[0] &&
|
|
email_login &&
|
|
email_login_code &&
|
|
email_login_field) {
|
|
const tempCode = foundUser[0][email_login_field];
|
|
if (debug) {
|
|
console.log("apiLoginUser:tempCode:", tempCode);
|
|
}
|
|
if (!tempCode)
|
|
throw new Error("No code Found!");
|
|
const tempCodeArray = tempCode.split("-");
|
|
const [code, codeDate] = tempCodeArray;
|
|
const millisecond15mins = 1000 * 60 * 15;
|
|
if (Date.now() - Number(codeDate) > millisecond15mins) {
|
|
throw new Error("Code Expired");
|
|
}
|
|
isPasswordCorrect = code === email_login_code;
|
|
}
|
|
let socialUserValid = false;
|
|
if (!isPasswordCorrect && !socialUserValid) {
|
|
return {
|
|
success: false,
|
|
msg: "Wrong password, no social login validity",
|
|
payload: null,
|
|
};
|
|
}
|
|
if (debug) {
|
|
console.log("apiLoginUser:isPasswordCorrect:", isPasswordCorrect);
|
|
console.log("apiLoginUser:email_login:", email_login);
|
|
}
|
|
if (isPasswordCorrect && email_login) {
|
|
const resetTempCode = yield (0, varDatabaseDbHandler_1.default)({
|
|
queryString: `UPDATE ${dbFullName}.users SET ${email_login_field} = '' WHERE email = ? OR username = ?`,
|
|
queryValuesArray: [email, username],
|
|
database: dbFullName,
|
|
useLocal,
|
|
});
|
|
}
|
|
let csrfKey = Math.random().toString(36).substring(2) +
|
|
"-" +
|
|
Math.random().toString(36).substring(2);
|
|
let userPayload = {
|
|
id: foundUser[0].id,
|
|
first_name: foundUser[0].first_name,
|
|
last_name: foundUser[0].last_name,
|
|
username: foundUser[0].username,
|
|
email: foundUser[0].email,
|
|
phone: foundUser[0].phone,
|
|
social_id: foundUser[0].social_id,
|
|
image: foundUser[0].image,
|
|
image_thumbnail: foundUser[0].image_thumbnail,
|
|
verification_status: foundUser[0].verification_status,
|
|
social_login: foundUser[0].social_login,
|
|
social_platform: foundUser[0].social_platform,
|
|
csrf_k: csrfKey,
|
|
more_data: foundUser[0].more_user_data,
|
|
logged_in_status: true,
|
|
date: Date.now(),
|
|
};
|
|
if (debug) {
|
|
console.log("apiLoginUser:userPayload:", userPayload);
|
|
console.log("apiLoginUser:Sending Response Object ...");
|
|
}
|
|
const resposeObject = {
|
|
success: true,
|
|
msg: "Login Successful",
|
|
payload: userPayload,
|
|
userId: foundUser[0].id,
|
|
csrf: csrfKey,
|
|
};
|
|
if (additionalFields &&
|
|
Array.isArray(additionalFields) &&
|
|
additionalFields.length > 0) {
|
|
additionalFields.forEach((key) => {
|
|
userPayload[key] = foundUser[0][key];
|
|
});
|
|
}
|
|
return resposeObject;
|
|
});
|
|
}
|