1
1
import { Intent } from '@blueprintjs/core' ;
2
+ import { GoogleOAuthProvider , SuccessTokenResponse } from 'google-oauth-gsi' ;
2
3
import { Chapter , Variant } from 'js-slang/dist/types' ;
3
4
import { SagaIterator } from 'redux-saga' ;
4
5
import { call , put , select } from 'redux-saga/effects' ;
@@ -39,7 +40,15 @@ const MIME_SOURCE = 'text/plain';
39
40
// const MIME_FOLDER = 'application/vnd.google-apps.folder';
40
41
41
42
// GIS Token Client
42
- let tokenClient : google . accounts . oauth2 . TokenClient ;
43
+ let googleProvider : GoogleOAuthProvider ;
44
+ // Login function
45
+ const googleLogin = ( ) => new Promise < SuccessTokenResponse > ( ( resolve , reject ) => {
46
+ googleProvider . useGoogleLogin ( {
47
+ flow : 'implicit' ,
48
+ onSuccess : resolve ,
49
+ scope : SCOPES ,
50
+ } ) ( )
51
+ } ) ;
43
52
44
53
export function * persistenceSaga ( ) : SagaIterator {
45
54
yield takeLatest ( LOGOUT_GOOGLE , function * ( ) : any {
@@ -51,7 +60,8 @@ export function* persistenceSaga(): SagaIterator {
51
60
52
61
yield takeLatest ( LOGIN_GOOGLE , function * ( ) : any {
53
62
yield call ( ensureInitialised ) ;
54
- yield call ( getToken ) ;
63
+ yield call ( googleLogin ) ;
64
+ yield call ( handleUserChanged , gapi . client . getToken ( ) . access_token ) ;
55
65
} ) ;
56
66
57
67
yield takeEvery ( PERSISTENCE_INITIALISE , function * ( ) : any {
@@ -327,18 +337,13 @@ const initialisationPromise: Promise<void> = new Promise(res => {
327
337
328
338
// only called once
329
339
async function initialise ( ) {
330
- // load GIS script
331
- // adapted from https://github.com/MomenSherif/react-oauth
332
- await new Promise < void > ( ( resolve , reject ) => {
333
- const scriptTag = document . createElement ( 'script' ) ;
334
- scriptTag . src = 'https://accounts.google.com/gsi/client' ;
335
- scriptTag . async = true ;
336
- scriptTag . defer = true ;
337
- scriptTag . onload = ( ) => resolve ( ) ;
338
- scriptTag . onerror = ev => {
339
- reject ( ev ) ;
340
- } ;
341
- document . body . appendChild ( scriptTag ) ;
340
+ // initialize GIS client
341
+ googleProvider = new GoogleOAuthProvider ( {
342
+ clientId : Constants . googleClientId ! ,
343
+ onScriptLoadError : ( ) => console . log ( 'onScriptLoadError' ) ,
344
+ onScriptLoadSuccess : ( ) => {
345
+ console . log ( 'onScriptLoadSuccess' ) ;
346
+ } ,
342
347
} ) ;
343
348
344
349
// load and initialize gapi.client
@@ -352,18 +357,7 @@ async function initialise() {
352
357
discoveryDocs : DISCOVERY_DOCS
353
358
} ) ;
354
359
355
- // initialize GIS client
356
- await new Promise < google . accounts . oauth2 . TokenClient > ( ( resolve , reject ) => {
357
- resolve (
358
- window . google . accounts . oauth2 . initTokenClient ( {
359
- client_id : Constants . googleClientId ! ,
360
- scope : SCOPES ,
361
- callback : ( ) => void 0 // will be updated in getToken()
362
- } )
363
- ) ;
364
- } ) . then ( c => {
365
- tokenClient = c ;
366
- } ) ;
360
+
367
361
}
368
362
369
363
function * handleUserChanged ( accessToken : string | null ) {
@@ -380,27 +374,6 @@ function* handleUserChanged(accessToken: string | null) {
380
374
}
381
375
}
382
376
383
- // adapted from https://developers.google.com/identity/oauth2/web/guides/migration-to-gis
384
- function * getToken ( ) {
385
- yield new Promise ( ( resolve , reject ) => {
386
- try {
387
- // Settle this promise in the response callback for requestAccessToken()
388
- ( tokenClient as any ) . callback = ( resp : google . accounts . oauth2 . TokenResponse ) => {
389
- if ( resp . error !== undefined ) {
390
- reject ( resp ) ;
391
- }
392
- // GIS has already automatically updated gapi.client
393
- // with the newly issued access token by this point
394
- resolve ( resp ) ;
395
- } ;
396
- tokenClient . requestAccessToken ( ) ;
397
- } catch ( err ) {
398
- reject ( err ) ;
399
- }
400
- } ) ;
401
- yield call ( handleUserChanged , gapi . client . getToken ( ) . access_token ) ;
402
- }
403
-
404
377
function * ensureInitialised ( ) {
405
378
startInitialisation ( ) ;
406
379
yield initialisationPromise ;
@@ -412,13 +385,15 @@ function* ensureInitialisedAndAuthorised() {
412
385
const currToken : GoogleApiOAuth2TokenObject = yield call ( gapi . client . getToken ) ;
413
386
414
387
if ( currToken === null ) {
415
- yield call ( getToken ) ;
388
+ yield call ( googleLogin ) ;
389
+ yield call ( handleUserChanged , gapi . client . getToken ( ) . access_token ) ;
416
390
} else {
417
391
// check if loaded token is still valid
418
392
const email : string | undefined = yield call ( getUserProfileDataEmail ) ;
419
393
const isValid = email ? true : false ;
420
394
if ( ! isValid ) {
421
- yield call ( getToken ) ;
395
+ yield call ( googleLogin ) ;
396
+ yield call ( handleUserChanged , gapi . client . getToken ( ) . access_token ) ;
422
397
}
423
398
}
424
399
}
0 commit comments