Created
April 17, 2018 13:37
-
-
Save rawnly/03e3e4c40ae3d771d9fd456198da2be5 to your computer and use it in GitHub Desktop.
GitHub nodejs login via user credentials
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const got = require('got'); | |
const chalk = require('chalk'); | |
(async () => { | |
console.clear(); | |
const [username, password] = process.argv.splice(2); | |
if ( username.includes('-') ) { | |
const flag = username.replace(/\-+/, ''); | |
if ( flag === 'help' || flag === 'h' ) { | |
console.log(); | |
console.log(chalk`{bold Usage}`) | |
console.log(chalk` {green $} {yellow git-info <username> <password>}`) | |
} else console.log(flag); | |
process.exit(); | |
} | |
try { | |
const { body } = await got('https://api.github.com/user', { | |
headers: { 'Authorization': 'Basic ' + btoa(username + ':' + password) } | |
}); | |
const user = JSON.parse(body); | |
console.log(chalk`Welcome {bold {rgb(0, 128, 255) ${user.login}}}, your profile id is: "{dim ${user.id}}"`) | |
console.log(chalk`Your plan is: {underline ${user.plan.name}} and you have {yellow ${user.plan.private_repos}} private repos`); | |
console.log(); | |
} catch (error) { | |
if ( !error.statusMessage ) { | |
throw error; | |
} | |
console.log(); | |
console.log(chalk`{red {bold Error}}: ${error.statusMessage}`); | |
} | |
})() | |
/** | |
* @description string to base64 | |
* @param {String} string | |
*/ | |
function btoa(string) { | |
return Buffer.from(string).toString('base64') | |
} | |
/** | |
* @description base64 to string | |
* @param {String} ecoded | |
*/ | |
function atob(ecoded) { | |
return Buffer.from(encoded, 'base64').toString() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment