{"size": 954, "ext": "js", "max_stars_repo_path": "sample/interactive-pkce.js", "max_stars_repo_name": "appcelerator/amplify-cli-auth", "max_stars_repo_stars_event_min_datetime": "2018-04-25T18:08:45.000Z", "max_stars_repo_stars_event_max_datetime": "2018-04-25T18:08:45.000Z", "max_issues_repo_path": "sample/interactive-pkce.js", "max_issues_repo_name": "appcelerator/amplify-cli-auth", "max_issues_repo_issues_event_min_datetime": "2018-04-24T21:54:10.000Z", "max_issues_repo_issues_event_max_datetime": "2018-04-24T21:55:03.000Z", "max_forks_repo_path": "sample/interactive-pkce.js", "max_forks_repo_name": "appcelerator/amplify-cli-auth", "max_forks_repo_forks_event_min_datetime": "2018-05-04T10:27:44.000Z", "max_forks_repo_forks_event_max_datetime": "2018-05-04T10:27:44.000Z"}
{"max_stars_count": 1, "max_issues_count": 2, "max_forks_count": 1, "avg_line_length": 21.6818181818, "max_line_length": 69, "alphanum_fraction": 0.6121593291}
'use strict';

const http = require('http');
const querystring = require('query-string');
const opn = require('opn');

const auth = require('amplify-auth-library');

const client = auth.client({
	realm: '###',
	clientId: '###',
	redirectUri: '###',
	baseUrl: '###'
});

const authUrl = client.generateAuthorizeUrl();

const server = http.createServer((req, res) => {
	console.log('req.url: ', req.url);
	if (req.url.indexOf('/callback') > -1) {
		const qs = querystring.parse(req.url);
		res.end('Authorization successful! Please return to the console.');
		server.close();

		console.log('code: ', qs.code);

		client.getToken(qs.code)
			.then(result => {
				return client.getUserInfo(result.access_token);
			})
			.then(info => {
				return client.refreshToken();
			})
			.then(result => {
				process.exit(0);
			})
			.catch(err => {
				process.exit(0);
			});
	}
}).listen(3000, () => {
	opn(authUrl, {wait: false}).then(cp => cp.unref());
});
