diff options
Diffstat (limited to 'src/scripts/user-authentication.ts')
| -rw-r--r-- | src/scripts/user-authentication.ts | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/scripts/user-authentication.ts b/src/scripts/user-authentication.ts new file mode 100644 index 00000000..e9d13091 --- /dev/null +++ b/src/scripts/user-authentication.ts @@ -0,0 +1,65 @@ +///<reference path="../../typings/index.d.ts" /> +import * as $ from "jquery"; + + +// Redirect the user to the splash page, if not signed in +if (localStorage.getItem("googleToken") === null) { + window.location.replace("/"); +} + +// Set the username in the navbar +$("nav .user .username").text(localStorage.getItem("googleName")); + + +// Set the language of the GAuth button to be English +window["___gcfg"] = { + lang: 'en' +}; + +/** + * Google signin button. + */ +window["gapiSigninButton"] = () => { + gapi.signin2.render('google-signin', { + 'scope': 'profile email', + 'onsuccess': (googleUser) => { + const auth2 = gapi.auth2.getAuthInstance(); + + // Handle signout click + $("nav .user .sign-out").click(() => { + removeUserInfo(); + auth2.signOut().then(() => { + window.location.href = "/"; + }); + }); + + // Check if the token has expired + const currentTime = (new Date()).getTime() / 1000; + + if (parseInt(localStorage.getItem("googleTokenExpiration")) - currentTime <= 0) { + auth2.signIn().then(() => { + localStorage.setItem("googleToken", googleUser.getAuthResponse().id_token); + const expirationTime = (new Date()).getTime() / 1000 + parseInt(googleUser.getAuthResponse().expires_in) - 5; + localStorage.setItem("googleTokenExpiration", expirationTime.toString()); + }); + } + }, + 'onfailure': () => { + window.location.href = "/"; + console.log("Oops, something went wrong with your Google signin... Try again?"); + } + }); +}; + + +/** + * Removes session storage items. + */ +export function removeUserInfo() { + localStorage.removeItem("googleToken"); + localStorage.removeItem("googleTokenExpiration"); + localStorage.removeItem("googleName"); + localStorage.removeItem("googleEmail"); + localStorage.removeItem("userId"); + localStorage.removeItem("simulationId"); +} |
