Skip to content

Commit

Permalink
Added login expiring after 4 days for re-auth
Browse files Browse the repository at this point in the history
  • Loading branch information
imprakharshukla committed Aug 30, 2020
1 parent bb0e27d commit cb6fa02
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 77 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,5 @@ dist
.idea/jsLibraryMappings.xml
.idea/misc.xml
.idea/modules.xml

JsonModules/
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,5 @@ dist
.pnp.*

/Scripts
/JsonModules

81 changes: 61 additions & 20 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');


Expand All @@ -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)
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -283,5 +323,6 @@ module.exports = {
signInIfUserExists,
errorLog,
successLog,
processingLog
processingLog,
checkIfLoginValid
}
52 changes: 24 additions & 28 deletions commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -16,7 +16,8 @@ const {
successLog,
errorLog,
processingLog,
login
login,
checkIfLoginValid
} = require('./app')

const questions = [
Expand All @@ -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')
Expand All @@ -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
Expand All @@ -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'.")
}
})

Expand All @@ -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'.")
}
})

Expand Down
Loading

0 comments on commit cb6fa02

Please sign in to comment.