Skip to content

Commit a2099f5

Browse files
committed
initial commit
1 parent 73b88ac commit a2099f5

7 files changed

Lines changed: 88 additions & 52 deletions

File tree

gatsby-node.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,10 @@ exports.onPostBuild = () => {
3434
}
3535
})
3636
}
37+
38+
exports.onCreateDevServer = ({ app }) => {
39+
app.use((req, res, next) => {
40+
res.setHeader("Cross-Origin-Opener-Policy", "unsafe-none")
41+
next()
42+
})
43+
}

gatsby/onInitialClientRender.js

Lines changed: 67 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,82 @@ import loadScript from "load-script"
22
import { store } from "./wrapRootElement"
33

44
export 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-
}
105

11-
var SCOPES = ["https://www.googleapis.com/auth/analytics.readonly"]
6+
const gapiPromise = new Promise((resolve, reject) => {
7+
loadScript("https://apis.google.com/js/api.js", err => {
8+
if (err) {
9+
reject(err)
10+
} else {
11+
window.gapi.load("client", () => {
12+
resolve(window.gapi)
13+
})
14+
}
15+
})
16+
})
17+
18+
const gisPromise = new Promise((resolve, reject) => {
19+
loadScript("https://accounts.google.com/gsi/client", err => {
20+
if (err) {
21+
reject(err)
22+
} else resolve(window.google)
23+
})
24+
})
25+
26+
Promise.all([gapiPromise, gisPromise])
27+
.then(([gapi, google]) => {
28+
const SCOPES = ["https://www.googleapis.com/auth/analytics.readonly"]
29+
const clientId = process.env.GAPI_CLIENT_ID
1230

13-
const clientId = process.env.GAPI_CLIENT_ID
31+
if (!clientId) {
32+
console.error(
33+
"GAPI_CLIENT_ID is not defined. Please check your .env file."
34+
)
35+
store.dispatch({ type: "gapiStatus", status: "cannot initialize" })
36+
return
37+
}
1438

15-
// TODO - Remove :analytics and replace it with the discovery document.
16-
window.gapi.load("client:auth2:analytics", () => {
1739
Promise.all([
18-
window.gapi.client.load(
19-
"https://analyticsreporting.googleapis.com/$discovery/rest?version=v4"
20-
),
21-
window.gapi.client.load(
40+
gapi.client.load(
2241
"https://analyticsdata.googleapis.com/$discovery/rest"
2342
),
24-
window.gapi.client.load(
43+
gapi.client.load(
2544
"https://analyticsadmin.googleapis.com/$discovery/rest"
2645
),
27-
]).then(() => {
28-
window.gapi.client
29-
.init({
46+
])
47+
.then(() => {
48+
console.log("Initializing token client...")
49+
// Replace gapi.auth2.init() with google.accounts.oauth2.initTokenClient()
50+
const tokenClient = google.accounts.oauth2.initTokenClient({
51+
client_id: clientId,
3052
scope: SCOPES.join(" "),
31-
clientId,
32-
})
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-
})
46-
})
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)
53+
callback: tokenResponse => {
54+
console.log("onInitialClientRender: callback invoked", tokenResponse)
55+
if (tokenResponse && tokenResponse.access_token) {
56+
gapi.client.setToken(tokenResponse.access_token)
57+
console.log("onInitialClientRender: setUser", tokenResponse);
58+
store.dispatch({ type: "setUser", user: tokenResponse })
59+
} else {
60+
console.error("onInitialClientRender: tokenResponse did not contain access_token.", tokenResponse);
61+
store.dispatch({ type: "setUser", user: undefined })
62+
}
63+
}, error_callback: (error) => {
64+
console.error("GIS Error:", error);
65+
},
5866
})
59-
}, console.error)
67+
68+
store.dispatch({ type: "setGapi", gapi })
69+
store.dispatch({ type: "gapiStatus", status: "initialized" })
70+
store.dispatch({ type: "setGoogle", google })
71+
store.dispatch({ type: "setTokenClient", tokenClient })
72+
})
73+
.catch(e => {
74+
store.dispatch({ type: "setGapi", gapi })
75+
store.dispatch({ type: "setUser", user: undefined })
76+
store.dispatch({ type: "gapiStatus", status: "cannot initialize" })
77+
console.error(e)
78+
})
79+
})
80+
.catch(e => {
81+
console.error(e)
6082
})
61-
})
6283
}

gatsby/wrapRootElement.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@ type State =
2020
{
2121
user?: {},
2222
gapi?: PartialDeep<typeof gapi>,
23+
google?: any,
24+
tokenClient?: any,
2325
toast?: string,
24-
status?: string
26+
gapiStatus?: string
2527
}
2628

2729
type Action =
2830
| { type: 'setUser', user: {} | undefined }
2931
| { type: 'setGapi', gapi: PartialDeep<typeof gapi> | undefined }
32+
| { type: 'setGoogle', google: any }
33+
| { type: 'setTokenClient', tokenClient: any }
3034
| { type: 'setToast', toast: string | undefined }
3135
| { type: 'gapiStatus', status: string | undefined };
3236
const reducer = (state: State = {}, action: Action) => {
@@ -35,6 +39,10 @@ const reducer = (state: State = {}, action: Action) => {
3539
return { ...state, user: action.user }
3640
case "setGapi":
3741
return { ...state, gapi: action.gapi }
42+
case "setGoogle":
43+
return { ...state, google: action.google }
44+
case "setTokenClient":
45+
return { ...state, tokenClient: action.tokenClient }
3846
case "setToast":
3947
return { ...state, toast: action.toast }
4048
case "gapiStatus":

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,9 @@
6060
"@testing-library/react": "^14.0.0",
6161
"@testing-library/user-event": "^13.1.8",
6262
"@types/gapi": "^0.0.39",
63-
"@types/gapi.auth2": "^0.0.54",
6463
"@types/gapi.client.analytics": "^3.0.7",
6564
"@types/gapi.client.analyticsadmin": "^1.0.0",
6665
"@types/gapi.client.analyticsdata": "^1.0.2",
67-
"@types/gapi.client.analyticsreporting": "^4.0.3",
6866
"@types/gtag.js": "^0.0.5",
6967
"@types/jest": "^26.0.23",
7068
"@types/node": "^18.16.5",

src/components/Layout/Login.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { UserStatus } from "./useLogin"
77

88
interface LoginProps {
99
className?: string
10-
user: gapi.auth2.GoogleUser | undefined
10+
user: any
1111
userStatus: UserStatus
1212
login: () => void
1313
logout: () => void

src/components/Layout/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ interface TemplateProps {
6969
userStatus?: UserStatus
7070
login?: () => void
7171
logout?: () => void
72-
user?: gapi.auth2.GoogleUser
72+
user?: any
7373
}
7474

7575
const PREFIX = 'Layout2';

src/global.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ declare module "*.svg" {
2323
declare module "*.module.css";
2424

2525
declare interface AppState {
26-
user?: gapi.auth2.GoogleUser
26+
user?: User
2727
gapi?: typeof gapi
2828
gapiStatus?: string
2929
measurementID: string
30+
tokenClient?: any
31+
google?: typeof google
3032
}

0 commit comments

Comments
 (0)