You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cpgc/modules/api.js

88 lines
2.3 KiB
JavaScript

5 years ago
'use strict';
const { BrowserWindow, ipcRenderer } = require('electron');
const axios = require('axios');
let options = {
client_id: '552f169c185b172eda15',
client_secret: 'e4e065e669f64a60a979990000ea2b321303e1d0',
scopes: ["repo", "user:email"]
};
function handleGitHub() {
let authWindow = new BrowserWindow({
width: 600,
height: 800,
show: false,
'node-integration': true
});
let url = `https://github.com/login/oauth/authorize?client_id=${options.client_id}&scope=${options.scopes}`
authWindow.loadURL(url);
authWindow.show();
return authWindow;
}
function handleGitHubCallback(authWindow, url, mainWin) {
console.log('handleGitHubCallback hit');
let raw_code = /code=([^&]*)/.exec(url) || null;
let code = (raw_code && raw_code.length > 1) ? raw_code[1] : null;
let error = /\?error=(.+)$/.exec(url);
if(code || error) {
authWindow.destroy();
if(code) {
requestGitHubToken(code, mainWin);
} else {
console.log('Error in handleGitHubCallback');
}
}
}
function requestGitHubToken(code, mainWin) {
axios.post('https://github.com/login/oauth/access_token', {
client_id: options.client_id,
client_secret: options.client_secret,
code: code
})
.then((response) => {
let token = response.data.split('&')[0].split('=')[1];
5 years ago
mainWin.reload();
5 years ago
mainWin.webContents.send("token", token);
});
}
5 years ago
function getCollaborators(token, mainWin, repo) {
axios.get('https://api.github.com/user', {
headers: {
'Authorization': `token ${token}`
}
})
.then(response => {
let user = response.data.login;
axios.get(`https://api.github.com/repos/${user}/${repo}`, {
headers: {
'Authorization': `token ${token}`
}
})
.then(response => {
mainWin.webContents.send('gh_user', user);
mainWin.webContents.send('desc', response.data.description);
})
.catch(error => {
console.log(error);
});
})
.catch(error => {
console.log('error!');
});
}
5 years ago
module.exports = {
handleGitHub,
5 years ago
handleGitHubCallback,
getCollaborators
5 years ago
};