-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Thanks Google for making me waste half my day on this unnecessary work. Really appreciate it. google/google-api-javascript-client#260 #21
- Loading branch information
Showing
8 changed files
with
63 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,54 @@ | ||
"use strict"; | ||
|
||
import Cookies from "js-cookie"; | ||
import { v4 as getUUID } from "uuid"; | ||
|
||
import { fetchAction } from "./api"; | ||
import { thunk } from "./util"; | ||
|
||
// Check if the session is currently authenticated with the necessary | ||
// permissions. If it is and the UI is currently in an OAuth state, | ||
// move on to fetching the API data. If the UI has already moved on | ||
// from the OAuth states, do nothing. If the session isn't | ||
// authenticated, display the login screen so the user can trigger an | ||
// OAuth flow. | ||
const oauthCheckAuthAction = thunk((dispatch) => { | ||
dispatch({ type: "VERIFYING_OAUTH" }); | ||
|
||
// Only get auth instances if OAuth token isn't in cookie | ||
if (Cookies.get("oauthToken") === undefined) { | ||
const GoogleAuth = gapi.auth2.getAuthInstance(); | ||
if (GoogleAuth.currentUser.get().hasGrantedScopes("email")) { | ||
dispatch(fetchAction); | ||
} else { | ||
dispatch({ type: "PROMPT_FOR_LOGIN" }); | ||
} | ||
} else { | ||
dispatch(fetchAction); | ||
} | ||
}); | ||
|
||
// Set up OAuth, check auth status, and arrange for auth status to be | ||
// re-checked when the Google client library says the user has logged | ||
// in. | ||
export const oauthSetupAction = thunk(async (dispatch) => { | ||
dispatch({ type: "VERIFYING_OAUTH" }); | ||
|
||
// Only get auth instances if OAuth token isn't in cookie | ||
if (Cookies.get("oauthToken") === undefined) { | ||
// https://developers.google.com/identity/protocols/OAuth2UserAgent | ||
await new Promise((resolve) => gapi.load("client:auth2", resolve)); | ||
await gapi.client.init({ | ||
clientId: | ||
"548868103597-3th6ihbnejkscon1950m9mm31misvhk9.apps.googleusercontent.com", | ||
scope: "email", | ||
}); | ||
const GoogleAuth = gapi.auth2.getAuthInstance(); | ||
GoogleAuth.isSignedIn.listen(() => dispatch(oauthCheckAuthAction)); | ||
dispatch(oauthCheckAuthAction); | ||
dispatch({ type: "PROMPT_FOR_LOGIN" }); | ||
} else { | ||
dispatch(oauthCheckAuthAction); | ||
dispatch(fetchAction); | ||
} | ||
}); | ||
|
||
// Start the OAuth login flow, and update the UI to have some helpful | ||
// info in case the user closes the popup. | ||
export const oauthLoginAction = thunk(async (dispatch) => { | ||
dispatch({ type: "WAIT_FOR_LOGIN" }); | ||
const GoogleAuth = gapi.auth2.getAuthInstance(); | ||
try { | ||
await GoogleAuth.signIn({ prompt: "select_account" }); | ||
} catch (error) { | ||
// e.g., user closed login popup | ||
|
||
// https://developers.google.com/identity/protocols/oauth2/javascript-implicit-flow#javascript-sample-code | ||
// | ||
// We're making a GET request using a <form> element because... some | ||
// kind of bizarre hack around CORS restrictions? Dunno, that's what | ||
// Google told me to do. | ||
|
||
const form = document.createElement("form"); | ||
form.setAttribute("method", "GET"); | ||
form.setAttribute("action", "https://accounts.google.com/o/oauth2/v2/auth"); | ||
|
||
const params = { | ||
client_id: | ||
"548868103597-3th6ihbnejkscon1950m9mm31misvhk9.apps.googleusercontent.com", | ||
redirect_uri: document.location.origin + "/oauth", | ||
response_type: "id_token", | ||
nonce: getUUID(), | ||
scope: "email", | ||
}; | ||
|
||
for (const [key, value] of Object.entries(params)) { | ||
const input = document.createElement("input"); | ||
input.setAttribute("type", "hidden"); | ||
input.setAttribute("name", key); | ||
input.setAttribute("value", value); | ||
form.appendChild(input); | ||
} | ||
|
||
document.body.appendChild(form); | ||
form.submit(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"use strict"; | ||
|
||
import Cookies from "js-cookie"; | ||
|
||
const oauthToken = new URLSearchParams( | ||
decodeURIComponent(document.location.hash.slice(1)), | ||
).get("id_token"); | ||
|
||
Cookies.set("oauthToken", oauthToken, { expires: 999999 }); | ||
|
||
window.location.replace("/"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>Redirecting...</title> | ||
</head> | ||
<script defer src="./js/oauth/redirect.js"></script> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters