From cb6fa02e448783c3377f8ecdfe815beed8f96d56 Mon Sep 17 00:00:00 2001 From: Prakhar Date: Sun, 30 Aug 2020 12:41:16 +0530 Subject: [PATCH] Added login expiring after 4 days for re-auth --- .gitignore | 2 + .npmignore | 1 + app.js | 81 +++++++++++++++++------ commands.js | 52 +++++++-------- package-lock.json | 162 ++++++++++++++++++++++++++++++++++++++-------- package.json | 6 +- 6 files changed, 227 insertions(+), 77 deletions(-) diff --git a/.gitignore b/.gitignore index 0c7912e..1ab4da8 100644 --- a/.gitignore +++ b/.gitignore @@ -123,3 +123,5 @@ dist .idea/jsLibraryMappings.xml .idea/misc.xml .idea/modules.xml + +JsonModules/ diff --git a/.npmignore b/.npmignore index 494eed5..519c28b 100644 --- a/.npmignore +++ b/.npmignore @@ -117,4 +117,5 @@ dist .pnp.* /Scripts +/JsonModules diff --git a/app.js b/app.js index 40baa90..da1c8af 100644 --- a/app.js +++ b/app.js @@ -7,13 +7,13 @@ const auth = firebase.auth() const chalk = require('chalk'); const fs = require('fs'); let allColors = "blue, black, red, green, orange, violet" - const open = require('open'); const axios = require('axios'); const path = require("path"); const {prompt} = require('inquirer') const {styleCommandLog, styleErrorLog} = require('./LoggingModules/commandListLogging') const userJsonFilePath = path.resolve(__dirname, "JsonModules", "JsonModules", "AppFiles", "user.json") +const configJsonFilePath = path.resolve(__dirname, "JsonModules", "JsonModules", "AppFiles", "config.json") const hexCodes = require('./LoggingModules/hexCodes.json'); @@ -29,14 +29,17 @@ const getUserInfo = () => { }) } catch (e) { - errorLog("Please login in before adding commands with 'andronix-cli login'.") + errorLog("Please login in before adding commands with 'acommands login'.") } } -const logout = () => { +const logout = (forced) => { auth.signOut().then(() => { - successLog("Logged out!") + if (!forced) + successLog("Logged out!") + else + errorLog("Login has expired. Please login again with 'acommands login' to continue.") //deleting the user record try { fs.unlinkSync(userJsonFilePath) @@ -139,22 +142,30 @@ const getCommands = (color) => { } const addCommands = async (commandObj) => { - let uid = auth.currentUser.uid - if (isObjFilled(commandObj)) { - try { - let docReference = await firestore.collection("users").doc(uid).collection("commands").add({ - com: commandObj.command.toString(), - dis: commandObj.description.toString(), - color: hexCodes[(commandObj.color).toString().toLowerCase()] - }) - successLog(`Command Added ${docReference.id}`) - stop() - } catch (e) { - errorLog("Error adding command!") + try { + let isLoginValid = await checkIfLoginValid() + console.log({isLoginValid}) + if (isLoginValid) { + let uid = auth.currentUser.uid + if (isObjFilled(commandObj)) { + try { + let docReference = await firestore.collection("users").doc(uid).collection("commands").add({ + com: commandObj.command.toString(), + dis: commandObj.description.toString(), + color: hexCodes[(commandObj.color).toString().toLowerCase()] + }) + successLog(`Command Added ${docReference.id}`) + stop() + } catch (e) { + errorLog("Error adding command!") + } + } else { + errorLog("Please provide all the values i.e a command, a description and a color of your choice.") + stop() + } } - } else { - errorLog("Please provide all the values i.e a command, a description and a color of your choice.") - stop() + } catch (e) { + errorLog(e) } } const login = async () => { @@ -203,6 +214,8 @@ async function loginUser(token) { } successLog("User logged in") successLog(`Welcome ${auth.currentUser.email}`) + let currentTime = new Date().valueOf().toString() + writeToConfig("loginTime", currentTime) stop() }).catch(e => { errorLog("Error logging in the user." + e) @@ -246,6 +259,33 @@ function isObjFilled(object) { } +const checkIfLoginValid = async function checkIfLoginValid() { + return new Promise(async (resolve, reject) => { + fs.readFile(configJsonFilePath, async (err, data) => { + if (err) { + errorLog("Error reading the login config.") + reject(false) + } + let dataFromFile = JSON.parse(data.toString()); + let loginTime = dataFromFile.loginTime; + let currentTime = new Date().valueOf() + //4 days + if (currentTime - loginTime >= 345600000) { + logout(true) + reject(false) + } else { + resolve(true) + } + }); + } + ) +} + +function writeToConfig(key, value) { + let dataToWrite = JSON.stringify({[key]: value}) + fs.writeFileSync(configJsonFilePath, dataToWrite); +} + const errorLog = function errorLog(e) { console.log(chalk.red(e)) } @@ -283,5 +323,6 @@ module.exports = { signInIfUserExists, errorLog, successLog, - processingLog + processingLog, + checkIfLoginValid } \ No newline at end of file diff --git a/commands.js b/commands.js index ebe0f0f..1ceaf96 100644 --- a/commands.js +++ b/commands.js @@ -2,7 +2,7 @@ const program = require('commander') const {prompt} = require('inquirer') -const chalk = require('chalk'); +const packageJson = require('./package.json'); let allColors = "blue, black, red, green, orange, violet" @@ -16,7 +16,8 @@ const { successLog, errorLog, processingLog, - login + login, + checkIfLoginValid } = require('./app') const questions = [ @@ -37,6 +38,11 @@ const questions = [ choices: ['Blue', 'Red', 'Green', 'Black', 'Violet', 'Orange'], } ] +const removeQuestion = { + name: 'docID', + type: 'input', + message: 'Please enter the ID of the command you want to delete.', +} program .command('login') @@ -58,29 +64,22 @@ program }) program - .version('1.0.0') + .version(packageJson.version) .description('Andronix Commands CLI') program .command('remove') .alias('r') .description("Remove a command") - .action(() => { + .action(async () => { try { - signInIfUserExists().then(r => { - prompt({ - name: 'docID', - type: 'input', - message: 'Please enter the ID of the command you want to delete.', - }).then(docID => removeCommands(docID)) - }).catch(e => { - errorLog(e) - }) - + await signInIfUserExists() + let isLoginValid = await checkIfLoginValid() + if (isLoginValid) + prompt(removeQuestion).then(docID => removeCommands(docID)) } catch (e) { - errorLog("Please login in before adding commands with 'acommands login'.") + errorLog("Please login in before listing commands with 'acommands login'.") } - }) program @@ -90,13 +89,12 @@ program .description(`Get all the commands or filter with -c [${allColors}]`) .action(async (args) => { try { - signInIfUserExists().then(r => { + await signInIfUserExists() + let isLoginValid = await checkIfLoginValid() + if (isLoginValid) getCommands(args.color) - }).catch(e => { - errorLog(e) - }) } catch (e) { - errorLog("Please login in before adding commands with 'andronix-cli login'.") + errorLog("Please login in before listing commands with 'acommands login'.") } }) @@ -105,16 +103,14 @@ program .command('add') .alias('a') .description('Add commands') - .action(() => { + .action(async () => { try { - signInIfUserExists().then(r => { + await signInIfUserExists() + let isLoginValid = await checkIfLoginValid() + if (isLoginValid) prompt(questions).then(answers => addCommands(answers)) - }).catch(e => { - errorLog(e) - }) - } catch (e) { - errorLog("Please login in before adding commands with 'acommands login'.") + errorLog("Please login in before listing commands with 'acommands login'.") } }) diff --git a/package-lock.json b/package-lock.json index 3799905..1eee191 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "andronix-command-cli", - "version": "1.0.2", + "version": "1.0.5", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -374,14 +374,6 @@ "event-target-shim": "^5.0.0" } }, - "add-filename-increment": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/add-filename-increment/-/add-filename-increment-1.0.0.tgz", - "integrity": "sha512-pFV8VZX8jxuVMIycKvGZkWF/ihnUubu9lbQVnOnZWp7noVxbKQTNj7zG2y9fXdPcuZ6lAN3Drr517HaivGCjdQ==", - "requires": { - "strip-filename-increment": "^2.0.1" - } - }, "agent-base": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-6.0.1.tgz", @@ -460,6 +452,11 @@ "resolved": "https://registry.npmjs.org/arrify/-/arrify-2.0.1.tgz", "integrity": "sha512-3duEwti880xqi4eAMN8AyR4a0ByT90zoYdLlevfrvU43vb0YZwZVfxOgxWrLXXXpyugL0hNZc9G6BiB5B3nUug==" }, + "async": { + "version": "1.5.2", + "resolved": "https://registry.npmjs.org/async/-/async-1.5.2.tgz", + "integrity": "sha1-7GphrlZIDAw8skHJVhjiCJL5Zyo=" + }, "axios": { "version": "0.20.0", "resolved": "https://registry.npmjs.org/axios/-/axios-0.20.0.tgz", @@ -603,6 +600,11 @@ "mimic-response": "^1.0.0" } }, + "code-point-at": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/code-point-at/-/code-point-at-1.1.0.tgz", + "integrity": "sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c=" + }, "color-convert": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", @@ -688,12 +690,6 @@ "is-obj": "^2.0.0" } }, - "dotenv": { - "version": "8.2.0", - "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz", - "integrity": "sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==", - "dev": true - }, "duplexer3": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/duplexer3/-/duplexer3-0.1.4.tgz", @@ -1001,6 +997,11 @@ "through": "^2.3.6" } }, + "invert-kv": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/invert-kv/-/invert-kv-1.0.0.tgz", + "integrity": "sha1-EEqOSqym09jNFXqO+L+rLXo//bY=" + }, "is-ci": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/is-ci/-/is-ci-2.0.0.tgz", @@ -1139,6 +1140,14 @@ "package-json": "^6.3.0" } }, + "lcid": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/lcid/-/lcid-1.0.0.tgz", + "integrity": "sha1-MIrMr6C8SDo4Z7S28rlQYlHRuDU=", + "requires": { + "invert-kv": "^1.0.0" + } + }, "locate-path": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-5.0.0.tgz", @@ -1213,6 +1222,93 @@ "resolved": "https://registry.npmjs.org/mute-stream/-/mute-stream-0.0.8.tgz", "integrity": "sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==" }, + "nconf": { + "version": "0.10.0", + "resolved": "https://registry.npmjs.org/nconf/-/nconf-0.10.0.tgz", + "integrity": "sha512-fKiXMQrpP7CYWJQzKkPPx9hPgmq+YLDyxcG9N8RpiE9FoCkCbzD0NyW0YhE3xn3Aupe7nnDeIx4PFzYehpHT9Q==", + "requires": { + "async": "^1.4.0", + "ini": "^1.3.0", + "secure-keys": "^1.0.0", + "yargs": "^3.19.0" + }, + "dependencies": { + "ansi-regex": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", + "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=" + }, + "camelcase": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-2.1.1.tgz", + "integrity": "sha1-fB0W1nmhu+WcoCys7PsBHiAfWh8=" + }, + "cliui": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/cliui/-/cliui-3.2.0.tgz", + "integrity": "sha1-EgYBU3qRbSmUD5NNo7SNWFo5IT0=", + "requires": { + "string-width": "^1.0.1", + "strip-ansi": "^3.0.1", + "wrap-ansi": "^2.0.0" + } + }, + "is-fullwidth-code-point": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-1.0.0.tgz", + "integrity": "sha1-754xOG8DGn8NZDr4L95QxFfvAMs=", + "requires": { + "number-is-nan": "^1.0.0" + } + }, + "string-width": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz", + "integrity": "sha1-EYvfW4zcUaKn5w0hHgfisLmxB9M=", + "requires": { + "code-point-at": "^1.0.0", + "is-fullwidth-code-point": "^1.0.0", + "strip-ansi": "^3.0.0" + } + }, + "strip-ansi": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", + "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", + "requires": { + "ansi-regex": "^2.0.0" + } + }, + "wrap-ansi": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-2.1.0.tgz", + "integrity": "sha1-2Pw9KE3QV5T+hJc8rs3Rz4JP3YU=", + "requires": { + "string-width": "^1.0.1", + "strip-ansi": "^3.0.1" + } + }, + "y18n": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/y18n/-/y18n-3.2.1.tgz", + "integrity": "sha1-bRX7qITAhnnA136I53WegR4H+kE=" + }, + "yargs": { + "version": "3.32.0", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-3.32.0.tgz", + "integrity": "sha1-AwiOnr+edWtpdRYR0qXvWRSCyZU=", + "requires": { + "camelcase": "^2.0.1", + "cliui": "^3.0.3", + "decamelize": "^1.1.1", + "os-locale": "^1.4.0", + "string-width": "^1.0.1", + "window-size": "^0.1.4", + "y18n": "^3.2.0" + } + } + } + }, "node-fetch": { "version": "2.6.0", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz", @@ -1228,6 +1324,11 @@ "resolved": "https://registry.npmjs.org/normalize-url/-/normalize-url-4.5.0.tgz", "integrity": "sha512-2s47yzUxdexf1OhyRi4Em83iQk0aPvwTddtFz4hnSSw9dCEsLEGf6SwIO8ss/19S9iBb5sJaOuTvTGDeZI00BQ==" }, + "number-is-nan": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/number-is-nan/-/number-is-nan-1.0.1.tgz", + "integrity": "sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0=" + }, "once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", @@ -1253,6 +1354,14 @@ "is-wsl": "^2.1.1" } }, + "os-locale": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/os-locale/-/os-locale-1.4.0.tgz", + "integrity": "sha1-IPnxeuKe00XoveWDsT0gCYA8FNk=", + "requires": { + "lcid": "^1.0.0" + } + }, "os-tmpdir": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz", @@ -1431,6 +1540,11 @@ "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" }, + "secure-keys": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/secure-keys/-/secure-keys-1.0.0.tgz", + "integrity": "sha1-8MgtmKOxOah3aogIBQuCRDEIf8o=" + }, "semver": { "version": "6.3.0", "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", @@ -1472,11 +1586,6 @@ "ansi-regex": "^5.0.0" } }, - "strip-filename-increment": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/strip-filename-increment/-/strip-filename-increment-2.0.1.tgz", - "integrity": "sha512-+v5xsiTTsdYqkPj7qz1zlngIsjZedhHDi3xp/9bMurV8kXe9DAr732gNVqtt4X8sI3hOqS3nlFfps5gyVcux6w==" - }, "strip-json-comments": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-2.0.1.tgz", @@ -1611,6 +1720,11 @@ "string-width": "^4.0.0" } }, + "window-size": { + "version": "0.1.4", + "resolved": "https://registry.npmjs.org/window-size/-/window-size-0.1.4.tgz", + "integrity": "sha1-+OGqHuWlPsW/FR/6CXQqatdpeHY=" + }, "wrap-ansi": { "version": "6.2.0", "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-6.2.0.tgz", @@ -1626,14 +1740,6 @@ "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" }, - "write": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/write/-/write-2.0.0.tgz", - "integrity": "sha512-yam9TAqN8sAZokECAejo9HpT2j2s39OgK8i8yxadrFBVo+iSWLfnipRVFulfAw1d2dz5vSuGmlMHYRKG4fysOA==", - "requires": { - "add-filename-increment": "^1.0.0" - } - }, "write-file-atomic": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/write-file-atomic/-/write-file-atomic-3.0.3.tgz", diff --git a/package.json b/package.json index 7695967..5e08a48 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "andronix-command-cli", - "version": "1.0.4", + "version": "1.0.6", "description": "", "main": "index.js", "scripts": { @@ -9,6 +9,10 @@ "bin": { "acommands": "./commands.js" }, + "repository": { + "type": "git", + "url": "https://github.com/imprakharshukla/andronix-command-cli/tree/master" + }, "keywords": [], "engines": { "node": ">=6.0.0"