11import loadScript from "load-script"
22import { store } from "./wrapRootElement"
33
4+ const addTokenListener = gapi => {
5+ let token = gapi . client . getToken ( )
6+ let tokenListener = _token => { }
7+
8+ Object . defineProperty ( gapi . client , "token" , {
9+ get : ( ) => token ,
10+ set : newToken => {
11+ token = newToken
12+ tokenListener ( token )
13+ } ,
14+ } )
15+
16+ gapi . client . setTokenListener = listener => {
17+ tokenListener = listener
18+ }
19+ }
20+
21+ const handleCredentialResponse = response => {
22+ // The response object contains the JWT ID token in the `credential` field.
23+ // We can decode it to get user information.
24+ const user = JSON . parse ( atob ( response . credential . split ( "." ) [ 1 ] ) )
25+ console . log ( "user" , user )
26+ store . dispatch ( { type : "setUser" , user } )
27+ }
28+
429export const onInitialClientRender = ( ) => {
5- loadScript ( `https://apis.google.com/js/api.js` , err => {
6- if ( err ) {
7- console . error ( "Could not load gapi" )
8- return
9- }
30+ const gapiPromise = new Promise ( ( resolve , reject ) => {
31+ loadScript ( "https://apis.google.com/js/api.js" , err => {
32+ if ( err ) {
33+ reject ( err )
34+ } else {
35+ window . gapi . load ( "client" , ( ) => {
36+ addTokenListener ( window . gapi )
37+ resolve ( window . gapi )
38+ } )
39+ }
40+ } )
41+ } )
1042
11- var SCOPES = [ "https://www.googleapis.com/auth/analytics.readonly" ]
43+ const gisPromise = new Promise ( ( resolve , reject ) => {
44+ loadScript ( "https://accounts.google.com/gsi/client" , err => {
45+ if ( err ) {
46+ reject ( err )
47+ } else resolve ( window . google )
48+ } )
49+ } )
50+
51+ Promise . all ( [ gapiPromise , gisPromise ] )
52+ . then ( ( [ gapi , google ] ) => {
53+ const SCOPES = "https://www.googleapis.com/auth/analytics.readonly"
54+ const clientId = process . env . GATSBY_GAPI_CLIENT_ID
1255
13- const clientId = process . env . GAPI_CLIENT_ID
56+ if ( ! clientId ) {
57+ console . error (
58+ "GATSBY_GAPI_CLIENT_ID is not defined. Please check your .env file."
59+ )
60+ store . dispatch ( { type : "gapiStatus" , status : "cannot initialize" } )
61+ return
62+ }
1463
15- // TODO - Remove :analytics and replace it with the discovery document.
16- window . gapi . load ( "client:auth2:analytics" , ( ) => {
1764 Promise . all ( [
18- window . gapi . client . load (
19- "https://analyticsreporting.googleapis.com/$discovery/rest?version=v4"
20- ) ,
21- window . gapi . client . load (
65+ gapi . client . load (
2266 "https://analyticsdata.googleapis.com/$discovery/rest"
2367 ) ,
24- window . gapi . client . load (
68+ gapi . client . load (
2569 "https://analyticsadmin.googleapis.com/$discovery/rest"
2670 ) ,
27- ] ) . then ( ( ) => {
28- window . gapi . client
29- . init ( {
30- scope : SCOPES . join ( " " ) ,
31- clientId,
71+ ] )
72+ . then ( ( ) => {
73+ const tokenClient = google . accounts . oauth2 . initTokenClient ( {
74+ client_id : clientId ,
75+ scope : SCOPES ,
76+ callback : tokenResponse => {
77+ gapi . client . setToken ( tokenResponse )
78+ } ,
3279 } )
33- . then ( ( ) => {
34- store . dispatch ( { type : "setGapi" , gapi : window . gapi } )
35- const user = window . gapi . auth2 . getAuthInstance ( ) . currentUser . get ( )
36- store . dispatch ( {
37- type : "setUser" ,
38- user : user . isSignedIn ( ) ? user : undefined ,
39- } )
40- window . gapi . auth2 . getAuthInstance ( ) . currentUser . listen ( user => {
41- store . dispatch ( {
42- type : "setUser" ,
43- user : user . isSignedIn ( ) ? user : undefined ,
44- } )
45- } )
80+
81+ google . accounts . id . initialize ( {
82+ client_id : clientId ,
83+ callback : handleCredentialResponse ,
4684 } )
47- . catch ( e => {
48- store . dispatch ( { type : "setGapi" , gapi : window . gapi } )
49- store . dispatch ( {
50- type : "setUser" ,
51- user : undefined ,
52- } )
53- store . dispatch ( {
54- type : "gapiStatus" ,
55- status : "cannot initialize" ,
56- } )
57- console . error ( e )
85+
86+ gapi . client . setTokenListener ( token => {
87+ if ( token === null ) {
88+ store . dispatch ( { type : "setUser" , user : undefined } )
89+ }
5890 } )
59- } , console . error )
91+
92+ store . dispatch ( { type : "setGapi" , gapi } )
93+ store . dispatch ( { type : "setGoogle" , google } )
94+ store . dispatch ( { type : "setTokenClient" , tokenClient } )
95+ store . dispatch ( { type : "gapiStatus" , status : "initialized" } )
96+ } )
97+ . catch ( e => {
98+ console . error ( "gapi client.load error" , e )
99+ store . dispatch ( { type : "setGapi" , gapi : window . gapi } )
100+ store . dispatch ( { type : "gapiStatus" , status : "cannot initialize" } )
101+ } )
60102 } )
61- } )
62- }
103+ . catch ( err => {
104+ console . error ( "Could not load gapi or gis" , err )
105+ store . dispatch ( { type : "gapiStatus" , status : "cannot initialize" } )
106+ } )
107+ }
0 commit comments