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
/
login.js
108 lines (93 loc) · 3.01 KB
/
login.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
const {parse} = require('url')
const {remote, ipcRenderer} = require('electron')
const qs = require('qs')
const axios = require('axios')
const GOOGLE_AUTHORIZATION_URL = 'https://accounts.google.com/o/oauth2/v2/auth'
const GOOGLE_TOKEN_URL = 'https://www.googleapis.com/oauth2/v4/token'
const GOOGLE_PROFILE_URL = 'https://www.googleapis.com/userinfo/v2/me'
async function googleSignIn () {
const code = await signInWithPopup()
const tokens = await fetchAccessTokens(code)
const {id, email, name} = await fetchGoogleProfile(tokens.access_token)
console.log('after async fetch google profile')
const providerUser = {
uid: id,
email,
displayName: name,
idToken: tokens.id_token,
}
return ipcRenderer.send('authorized', providerUser)
}
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: '484818166811-l38imlkp6f9s2t4p0c8mt9vui0vf7f0q.apps.googleusercontent.com',
scope: 'profile email',
}
const authUrl = `${GOOGLE_AUTHORIZATION_URL}?${qs.stringify(urlParams)}`
function handleNavigation (url) {
const query = parse(url, true).query
if (query) {
if (query.error) {
reject(new Error(`There was an error: ${query.error}`))
} else if (query.code) {
authWindow.removeAllListeners('closed')
setImmediate(() => authWindow.close())
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)
})
}
async function fetchAccessTokens (code) {
console.log('code')
const response = await axios.post(GOOGLE_TOKEN_URL, qs.stringify({
code,
client_id: '484818166811-l38imlkp6f9s2t4p0c8mt9vui0vf7f0q.apps.googleusercontent.com',
redirect_uri: 'http://127.0.0.1:8000',
grant_type: 'authorization_code',
}), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
})
return response.data
}
async function fetchGoogleProfile (accessToken) {
console.log('fetch')
const response = await axios.get(GOOGLE_PROFILE_URL, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${accessToken}`,
},
})
return response.data
}
const google = document.getElementById('google-login')
google.addEventListener('click', () => {
console.log('cheese')
googleSignIn()
})
const guest = document.getElementById('guest-login')
guest.addEventListener('click', () => {
ipcRenderer.send('guest', 'guest')
})