Skip to content

Instantly share code, notes, and snippets.

@rawnly
Created April 17, 2018 13:37
Show Gist options
  • Save rawnly/03e3e4c40ae3d771d9fd456198da2be5 to your computer and use it in GitHub Desktop.
Save rawnly/03e3e4c40ae3d771d9fd456198da2be5 to your computer and use it in GitHub Desktop.
GitHub nodejs login via user credentials
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