Add Email Login

This commit is contained in:
Benjamin Toby 2024-07-13 07:23:46 +01:00
parent 90cc67992b
commit 24c2445ff8
5 changed files with 82 additions and 16 deletions

View File

@ -13,9 +13,19 @@ const varDatabaseDbHandler = require("../engine/utils/varDatabaseDbHandler");
* }} param0.payload * }} param0.payload
* @param {string[]} [param0.additionalFields] * @param {string[]} [param0.additionalFields]
* @param {import("../../types/database-schema.td").DSQL_DatabaseSchemaType} [param0.dbSchema] * @param {import("../../types/database-schema.td").DSQL_DatabaseSchemaType} [param0.dbSchema]
* @param {boolean} [param0.email_login]
* @param {string} [param0.email_login_code]
* @param {string | null} [param0.email_login_field]
* @returns * @returns
*/ */
async function loginLocalUser({ payload, additionalFields, dbSchema }) { async function loginLocalUser({
payload,
additionalFields,
dbSchema,
email_login,
email_login_code,
email_login_field,
}) {
try { try {
/** /**
* User auth * User auth
@ -23,7 +33,9 @@ async function loginLocalUser({ payload, additionalFields, dbSchema }) {
* @description Authenticate user * @description Authenticate user
*/ */
const { email, username, password } = payload; const email = payload.email;
const username = payload.username;
const password = payload.password;
const dbFullName = process.env.DSQL_DB_NAME || ""; const dbFullName = process.env.DSQL_DB_NAME || "";
const encryptionKey = process.env.DSQL_ENCRYPTION_KEY || ""; const encryptionKey = process.env.DSQL_ENCRYPTION_KEY || "";
@ -34,7 +46,11 @@ async function loginLocalUser({ payload, additionalFields, dbSchema }) {
* *
* @description Check input validity * @description Check input validity
*/ */
if (email?.match(/ /) || username?.match(/ /) || password?.match(/ /)) { if (
email?.match(/ /) ||
(username && username?.match(/ /)) ||
(password && password?.match(/ /))
) {
return { return {
success: false, success: false,
msg: "Invalid Email/Password format", msg: "Invalid Email/Password format",
@ -46,16 +62,20 @@ async function loginLocalUser({ payload, additionalFields, dbSchema }) {
* *
* @description Password hash * @description Password hash
*/ */
let hashedPassword = hashPassword({ let hashedPassword = password
? hashPassword({
password: password, password: password,
encryptionKey: encryptionKey, encryptionKey: encryptionKey,
}); })
: null;
//////////////////////////////////////// ////////////////////////////////////////
//////////////////////////////////////// ////////////////////////////////////////
//////////////////////////////////////// ////////////////////////////////////////
const tableSchema = dbSchema?.tables.find((tb) => tb?.tableName === "users"); const tableSchema = dbSchema?.tables.find(
(tb) => tb?.tableName === "users"
);
let foundUser = await varDatabaseDbHandler({ let foundUser = await varDatabaseDbHandler({
queryString: `SELECT * FROM users WHERE email = ? OR username = ?`, queryString: `SELECT * FROM users WHERE email = ? OR username = ?`,
@ -77,8 +97,17 @@ async function loginLocalUser({ payload, additionalFields, dbSchema }) {
let isPasswordCorrect = false; let isPasswordCorrect = false;
if (foundUser && foundUser[0]) { if (foundUser && foundUser[0] && !email_login) {
isPasswordCorrect = hashedPassword === foundUser[0].password; 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];
isPasswordCorrect = tempCode === email_login_code;
} }
let socialUserValid = false; let socialUserValid = false;
@ -99,7 +128,10 @@ async function loginLocalUser({ payload, additionalFields, dbSchema }) {
//////////////////////////////////////// ////////////////////////////////////////
//////////////////////////////////////// ////////////////////////////////////////
let csrfKey = Math.random().toString(36).substring(2) + "-" + Math.random().toString(36).substring(2); let csrfKey =
Math.random().toString(36).substring(2) +
"-" +
Math.random().toString(36).substring(2);
let userPayload = { let userPayload = {
id: foundUser[0].id, id: foundUser[0].id,
@ -120,7 +152,11 @@ async function loginLocalUser({ payload, additionalFields, dbSchema }) {
date: Date.now(), date: Date.now(),
}; };
if (additionalFields && Array.isArray(additionalFields) && additionalFields.length > 0) { if (
additionalFields &&
Array.isArray(additionalFields) &&
additionalFields.length > 0
) {
additionalFields.forEach((key) => { additionalFields.forEach((key) => {
// @ts-ignore // @ts-ignore
userPayload[key] = foundUser?.[0][key]; userPayload[key] = foundUser?.[0][key];

16
package-lock.json generated
View File

@ -1,16 +1,17 @@
{ {
"name": "datasquirel", "name": "datasquirel",
"version": "1.8.2", "version": "2.0.0",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "datasquirel", "name": "datasquirel",
"version": "1.8.2", "version": "2.0.0",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"dotenv": "^16.3.1", "dotenv": "^16.3.1",
"mysql": "^2.18.1" "mysql": "^2.18.1",
"nodemailer": "^6.9.14"
}, },
"bin": { "bin": {
"dsql-dump": "engine/dump.js", "dsql-dump": "engine/dump.js",
@ -83,6 +84,15 @@
"node": ">= 0.6" "node": ">= 0.6"
} }
}, },
"node_modules/nodemailer": {
"version": "6.9.14",
"resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.9.14.tgz",
"integrity": "sha512-Dobp/ebDKBvz91sbtRKhcznLThrKxKt97GI2FAlAyy+fk19j73Uz3sBXolVtmcXjaorivqsbbbjDY+Jkt4/bQA==",
"license": "MIT-0",
"engines": {
"node": ">=6.0.0"
}
},
"node_modules/process-nextick-args": { "node_modules/process-nextick-args": {
"version": "2.0.1", "version": "2.0.1",
"resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz",

View File

@ -27,7 +27,8 @@
"homepage": "https://datasquirel.com/", "homepage": "https://datasquirel.com/",
"dependencies": { "dependencies": {
"dotenv": "^16.3.1", "dotenv": "^16.3.1",
"mysql": "^2.18.1" "mysql": "^2.18.1",
"nodemailer": "^6.9.14"
}, },
"devDependencies": { "devDependencies": {
"@types/mysql": "^2.15.21" "@types/mysql": "^2.15.21"

View File

@ -44,6 +44,9 @@ const loginLocalUser = require("../engine/user/login-user");
* @param {http.ServerResponse} params.response - Http response object * @param {http.ServerResponse} params.response - Http response object
* @param {String} params.encryptionKey - Encryption Key * @param {String} params.encryptionKey - Encryption Key
* @param {String} params.encryptionSalt - Encryption Salt * @param {String} params.encryptionSalt - Encryption Salt
* @param {boolean} [params.email_login] - Email only Login
* @param {string} [params.email_login_code] - Email login code
* @param {string} [params.temp_code_field] - Database table field name for temporary code
* *
* @returns { Promise<AuthenticatedUser>} * @returns { Promise<AuthenticatedUser>}
*/ */
@ -55,11 +58,21 @@ async function loginUser({
response, response,
encryptionKey, encryptionKey,
encryptionSalt, encryptionSalt,
email_login,
email_login_code,
temp_code_field,
}) { }) {
const scheme = process.env.DSQL_HTTP_SCHEME; const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST; const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT; const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
const defaultTempLoginFieldName = "temp_login_code";
const emailLoginTempCodeFieldName = email_login
? temp_code_field
? temp_code_field
: defaultTempLoginFieldName
: null;
/** /**
* Check Encryption Keys * Check Encryption Keys
* *
@ -135,6 +148,9 @@ async function loginUser({
payload, payload,
additionalFields, additionalFields,
dbSchema, dbSchema,
email_login,
email_login_code,
email_login_field: emailLoginTempCodeFieldName,
}); });
} }
} else { } else {
@ -150,6 +166,9 @@ async function loginUser({
payload, payload,
database, database,
additionalFields, additionalFields,
email_login,
email_login_code,
email_login_field: emailLoginTempCodeFieldName,
}); });
const httpsRequest = ( const httpsRequest = (

View File

@ -114,7 +114,7 @@ async function get({ key, db, query, queryValues, tableName }) {
}, },
port: localHostPort || 443, port: localHostPort || 443,
hostname: localHost || "datasquirel.com", hostname: localHost || "datasquirel.com",
path: path, path: encodeURIComponent(path),
}, },
/** /**