4
4
dialog,
5
5
BrowserWindow,
6
6
session,
7
- ipcMain
7
+ ipcMain,
8
+ webContents
8
9
} = require ( 'electron' ) ;
9
10
10
11
// The splash screen is what appears while the app is loading
@@ -23,6 +24,7 @@ const MenuBuilder = require('./menu');
23
24
24
25
const path = require ( 'path' ) ;
25
26
// const fs = require('fs');
27
+ require ( 'dotenv' ) . config ( ) ;
26
28
27
29
const isDev =
28
30
process . env . NODE_ENV === 'development' || process . env . NODE_ENV === 'test' ;
@@ -224,20 +226,20 @@ app.on('web-contents-created', (event, contents) => {
224
226
selfHost ,
225
227
'http://localhost:5000' ,
226
228
'https://reactype.herokuapp.com' ,
227
- 'https://github.com/ ' ,
229
+ 'https://github.com' ,
228
230
'https://nextjs.org' ,
231
+ 'https://www.facebook.com' ,
229
232
'https://developer.mozilla.org'
230
233
] ;
231
- console . log ( 'parsed URL origin' , parsedUrl . origin ) ;
232
234
// Log and prevent the app from navigating to a new page if that page's origin is not whitelisted
233
235
if ( ! validOrigins . includes ( parsedUrl . origin ) ) {
234
236
console . error (
235
- `The application tried to navigate to the following address: '${ parsedUrl } '. This origin is not whitelisted and the attempt to navigate was blocked.`
237
+ `The application tried to navigate to the following address: '${ parsedUrl } '. This origin is not whitelisted attempt to navigate was blocked.`
236
238
) ;
237
239
// if the requested URL is not in the whitelisted array, then don't navigate there
238
240
event . preventDefault ( ) ;
239
241
return ;
240
- } else console . log ( `Successful navigation to ${ parsedUrl } ` ) ;
242
+ }
241
243
} ) ;
242
244
243
245
contents . on ( 'will-redirect' , ( event , navigationUrl ) => {
@@ -246,9 +248,10 @@ app.on('web-contents-created', (event, contents) => {
246
248
selfHost ,
247
249
'http://localhost:5000' ,
248
250
'https://reactype.herokuapp.com' ,
249
- 'https://github.com/ ' ,
251
+ 'https://github.com' ,
250
252
'https://nextjs.org' ,
251
- 'https://developer.mozilla.org'
253
+ 'https://developer.mozilla.org' ,
254
+ 'https://www.facebook.com' ,
252
255
] ;
253
256
254
257
// Log and prevent the app from redirecting to a new page
@@ -262,7 +265,7 @@ app.on('web-contents-created', (event, contents) => {
262
265
263
266
event . preventDefault ( ) ;
264
267
return ;
265
- } else console . log ( 'Successful link sent to browser' ) ;
268
+ }
266
269
} ) ;
267
270
268
271
// https://electronjs.org/docs/tutorial/security#11-verify-webview-options-before-creation
@@ -286,11 +289,11 @@ app.on('web-contents-created', (event, contents) => {
286
289
selfHost ,
287
290
'http://localhost:5000' ,
288
291
'https://reactype.herokuapp.com' ,
289
- 'https://github.com/' ,
290
292
'https://nextjs.org' ,
291
- 'https://developer.mozilla.org'
293
+ 'https://developer.mozilla.org' ,
294
+ 'https://github.com' ,
295
+ 'https://www.facebook.com'
292
296
] ;
293
- console . log ( 'parsed URL origin' , parsedUrl . origin ) ;
294
297
// Log and prevent the app from navigating to a new page if that page's origin is not whitelisted
295
298
if ( ! validOrigins . includes ( parsedUrl . origin ) ) {
296
299
console . error (
@@ -299,9 +302,7 @@ app.on('web-contents-created', (event, contents) => {
299
302
// if the requested URL is not in the whitelisted array, then don't navigate there
300
303
event . preventDefault ( ) ;
301
304
return ;
302
- } else console . log ( `Successful new window to ${ parsedUrl } ` ) ;
303
- // event.preventDefault();
304
- // return;
305
+ }
305
306
} ) ;
306
307
} ) ;
307
308
@@ -382,10 +383,11 @@ ipcMain.on('delete_cookie', event => {
382
383
383
384
// opens new window for github oauth when button on sign in page is clicked
384
385
ipcMain . on ( 'github' , event => {
385
- // your github applications credentials
386
+ // your applications credentials
387
+ const githubUrl = 'https://github.com/login/oauth/authorize?' ;
386
388
const options = {
387
- client_id : 'your_client_id' ,
388
- client_secret : 'your_client_secret' ,
389
+ client_id : process . env . GITHUB_ID ,
390
+ client_secret : process . env . GITHUB_SECRET ,
389
391
scopes : [ 'user:email' , 'notifications' ]
390
392
} ;
391
393
// create new browser window object with size, title, security options
@@ -402,15 +404,50 @@ ipcMain.on('github', event => {
402
404
zoomFactor : 1.0
403
405
}
404
406
} ) ;
405
- const githubUrl = 'https://github.com/login/oauth/authorize?' ;
407
+ // const authUrl =
408
+ // githubUrl + 'client_id=' + options.client_id + '&scope=' + options.scopes;
406
409
const authUrl =
407
- githubUrl + 'client_id=' + options . client_id + '&scope=' + options . scopes ;
408
- // redirects to relevant server endpoint
410
+ `${ githubUrl } client_id=${ process . env . GITHUB_ID } ` ;
409
411
github . loadURL ( authUrl ) ;
410
- // show window
411
412
github . show ( ) ;
413
+ const handleCallback = ( url ) => {
414
+
415
+ const raw_code = / c o d e = ( [ ^ & ] \* ) / . exec ( url ) || null ;
416
+ const code = raw_code && raw_code . length > 1 ? raw_code [ 1 ] : null ;
417
+ const error = / \? e r r o r = ( .+ ) \$ / . exec ( url ) ;
418
+
419
+ if ( code || error ) {
420
+ // Close the browser if code found or error
421
+ authWindow . destroy ( ) ;
422
+ }
423
+
424
+ // If there is a code, proceed to get token from github
425
+ if ( code ) {
426
+ self . requestGithubToken ( options , code ) ;
427
+ } else if ( error ) {
428
+ alert (
429
+ "Oops! Something went wrong and we couldn't" +
430
+ 'log you in using Github. Please try again.'
431
+ ) ;
432
+ }
433
+ }
434
+
435
+ github . webContents . on ( 'will-navigate' , ( e , url ) => handleCallback ( url ) ) ;
436
+
437
+ github . webContents . on ( 'did-finish-load' , ( e , url , a , b ) => {
438
+ github . webContents . selectAll ( ) ;
439
+ } )
440
+
441
+ github . webContents . on ( 'did-get-redirect-request' , ( e , oldUrl , newUrl ) => handleCallback ( newUrl ) ) ;
442
+
443
+ // Reset the authWindow on close
444
+ github . on ( 'close' , ( ) => authWindow = null , false ) ;
445
+
412
446
// if final callback is reached and we get a redirect from server back to our app, close oauth window
413
447
github . webContents . on ( 'will-redirect' , ( e , callbackUrl ) => {
448
+ const matches = callbackUrl . match ( / (?< = \? = ) .* / ) ;
449
+ const ssid = matches ? matches [ 0 ] : '' ;
450
+ callbackUrl = callbackUrl . replace ( / \? = .* / , '' ) ;
414
451
let redirectUrl = 'app://rse/' ;
415
452
if ( isDev ) {
416
453
redirectUrl = 'http://localhost:8080/' ;
@@ -423,6 +460,9 @@ ipcMain.on('github', event => {
423
460
message : 'Github Oauth Successful!'
424
461
} ) ;
425
462
github . close ( ) ;
463
+ win . webContents . executeJavaScript ( `window.localStorage.setItem('ssid', '${ ssid } ')` )
464
+ . then ( result => win . loadURL ( selfHost ) )
465
+ . catch ( err => console . log ( err ) )
426
466
}
427
467
} ) ;
428
468
} ) ;
0 commit comments