This repository has been archived by the owner on Jun 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
githubLogin.js
138 lines (119 loc) · 4.2 KB
/
githubLogin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// const {parse} = require('url')
// const {remote, ipcRenderer} = require('electron')
// const qs = require('qs')
// const axios = require('axios')
const GITHUB_AUTHORIZATION_URL = 'http://github.com/login/oauth/authorize'
const GITHUB_TOKEN_URL = 'https://github.com/login/oauth/access_token'
//perhaps the below url is causing overly general user info to return
const GITHUB_PROFILE_URL = `https://api.github.com/?`
async function githubSignIn () {
const code = await signInWithPopup()
const tokens = await fetchAccessTokens(code)
console.log('fetched token', tokens)
// const {id, email, name} = await fetchGithubProfile(tokens.access_token)
const responseData = await fetchGithubProfile(tokens.access_token, tokens);
console.log('response from fetchting profile', responseData)
// const providerUser = {
// uid: id,
// email,
// displayName: name,
// idToken: tokens.id_token,
// }
return ipcRenderer.send('authorized', responseData)
}
function signInWithPopup () {
return new Promise((resolve, reject) => {
const authWindow = new remote.BrowserWindow({
width: 500,
height: 600,
show: true,
})
// TODO: Generate and validate PKCE code_challenge value
const urlParams = {
response_type: 'code',
redirect_uri: 'http://127.0.0.1:8000',
client_id: '8fcf3e5c2d3d5dd78188',
client_secret: '0e102c56021e1aa28005b469b3c83ef7cb7e5b0e',
scope: ['user:email','read:user']
}
const authUrl = `${GITHUB_AUTHORIZATION_URL}?${qs.stringify(urlParams)}`
function handleNavigation (url) {
console.log('url!', url)
const query = parse(url, true).query
console.log(query)
if (query) {
if (query.error) {
reject(new Error(`There was an error: ${query.error}`))
} else if (query.code) {
// Login is complete
authWindow.removeAllListeners('closed')
setImmediate(() => authWindow.close())
// This is the authorization code we need to request tokens
resolve(query.code)
}
}
}
authWindow.on('closed', () => {
// TODO: Handle this smoothly
throw new Error('Auth window was closed by user')
})
authWindow.webContents.on('will-navigate', (event, url) => {
handleNavigation(url)
})
authWindow.webContents.on('did-get-redirect-request', (event, oldUrl, newUrl) => {
handleNavigation(newUrl)
})
authWindow.loadURL(authUrl)
})
}
//perhaps build another async await function just to grab the dotcom_user from cookie from header from request of access_token
//append dotcom_user to the end of the GITHUB_PROFILE_URL, and then use that to run fetchGithubProfile to get specific user data
async function fetchAccessTokens (code) {
console.log('code')
const reqHeader = await axios.post(GITHUB_TOKEN_URL, qs.stringify({
code,
client_id: '8fcf3e5c2d3d5dd78188',
redirect_uri: 'http://127.0.0.1:8000',
grant_type: 'authorization_code',
client_secret: '0e102c56021e1aa28005b469b3c83ef7cb7e5b0e',
scope: ['user:email','read:user']
}), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Set-Cookie': 'dotcom_user',
},
})
console.log('reqheaders', reqHeader.headers);
const response = await axios.post(GITHUB_TOKEN_URL, qs.stringify({
code,
client_id: '8fcf3e5c2d3d5dd78188',
redirect_uri: 'http://127.0.0.1:8000',
grant_type: 'authorization_code',
client_secret: '0e102c56021e1aa28005b469b3c83ef7cb7e5b0e',
scope: ['user:email','read:user']
}), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Set-Cookie': 'dotcom_user',
},
})
console.log('inside fetchgithub token', JSON.stringify(response));
//return response.data?
return response
}
async function fetchGithubProfile (accessToken, tokens) {
console.log('fetch')
const response = await axios.get(GITHUB_PROFILE_URL, {
headers: {
'Content-Type': 'application/json',
'Authorization': accessToken,
'Set-Cookie': 'dotcom_user'
},
})
return response
}
const github = document.getElementById('github-login')
github.addEventListener('click', () => {
console.log('clicked GitHub Login!')
githubSignIn()
})