Skip to content

Commit a7e31a3

Browse files
sumomomomomolinedoestrolling
authored andcommitted
Migrate to google-oauth-gsi
1 parent 764501e commit a7e31a3

File tree

3 files changed

+30
-49
lines changed

3 files changed

+30
-49
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"classnames": "^2.3.2",
5050
"flexboxgrid": "^6.3.1",
5151
"flexboxgrid-helpers": "^1.1.3",
52+
"google-oauth-gsi": "^4.0.0",
5253
"hastscript": "^9.0.0",
5354
"js-slang": "^1.0.52",
5455
"js-yaml": "^4.1.0",

src/commons/sagas/PersistenceSaga.tsx

Lines changed: 24 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Intent } from '@blueprintjs/core';
2+
import { GoogleOAuthProvider, SuccessTokenResponse } from 'google-oauth-gsi';
23
import { Chapter, Variant } from 'js-slang/dist/types';
34
import { SagaIterator } from 'redux-saga';
45
import { call, put, select } from 'redux-saga/effects';
@@ -39,7 +40,15 @@ const MIME_SOURCE = 'text/plain';
3940
// const MIME_FOLDER = 'application/vnd.google-apps.folder';
4041

4142
// 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+
});
4352

4453
export function* persistenceSaga(): SagaIterator {
4554
yield takeLatest(LOGOUT_GOOGLE, function* (): any {
@@ -51,7 +60,8 @@ export function* persistenceSaga(): SagaIterator {
5160

5261
yield takeLatest(LOGIN_GOOGLE, function* (): any {
5362
yield call(ensureInitialised);
54-
yield call(getToken);
63+
yield call(googleLogin);
64+
yield call(handleUserChanged, gapi.client.getToken().access_token);
5565
});
5666

5767
yield takeEvery(PERSISTENCE_INITIALISE, function* (): any {
@@ -327,18 +337,13 @@ const initialisationPromise: Promise<void> = new Promise(res => {
327337

328338
// only called once
329339
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+
},
342347
});
343348

344349
// load and initialize gapi.client
@@ -352,18 +357,7 @@ async function initialise() {
352357
discoveryDocs: DISCOVERY_DOCS
353358
});
354359

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+
367361
}
368362

369363
function* handleUserChanged(accessToken: string | null) {
@@ -380,27 +374,6 @@ function* handleUserChanged(accessToken: string | null) {
380374
}
381375
}
382376

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-
404377
function* ensureInitialised() {
405378
startInitialisation();
406379
yield initialisationPromise;
@@ -412,13 +385,15 @@ function* ensureInitialisedAndAuthorised() {
412385
const currToken: GoogleApiOAuth2TokenObject = yield call(gapi.client.getToken);
413386

414387
if (currToken === null) {
415-
yield call(getToken);
388+
yield call(googleLogin);
389+
yield call(handleUserChanged, gapi.client.getToken().access_token);
416390
} else {
417391
// check if loaded token is still valid
418392
const email: string | undefined = yield call(getUserProfileDataEmail);
419393
const isValid = email ? true : false;
420394
if (!isValid) {
421-
yield call(getToken);
395+
yield call(googleLogin);
396+
yield call(handleUserChanged, gapi.client.getToken().access_token);
422397
}
423398
}
424399
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7084,6 +7084,11 @@ glsl-tokenizer@^2.1.5:
70847084
dependencies:
70857085
through2 "^0.6.3"
70867086

7087+
google-oauth-gsi@^4.0.0:
7088+
version "4.0.0"
7089+
resolved "https://registry.yarnpkg.com/google-oauth-gsi/-/google-oauth-gsi-4.0.0.tgz#5564e97df5535af8c150909e0df9adcb32af2758"
7090+
integrity sha512-6A2QTSB4iPPfqd7spIOnHLhP4Iu8WeZ7REq+zM47nzIC805FwgOTFj5UsatKpMoNDsmb2xXG2GpKKVNBxbE9Pw==
7091+
70877092
gopd@^1.0.1:
70887093
version "1.0.1"
70897094
resolved "https://registry.yarnpkg.com/gopd/-/gopd-1.0.1.tgz#29ff76de69dac7489b7c0918a5788e56477c332c"

0 commit comments

Comments
 (0)