import * as React from "react";
import * as Scrivito from "scrivito";
import {
GoogleAPI,
GoogleLogin,
GoogleLogout,
googleGetAuthResponse
} from "react-google-oauth";
const CLIENT_ID =
"468795843720-6akgnpes5b4lo9582c7na52i7r2ac55k.apps.googleusercontent.com";
export const visitorLoggedInWithGoogle =
!!window.localStorage.getItem("loggedInWithGoogle");
function setGoogleVisitor({ idToken }) {
if (visitorLoggedInWithGoogle) {
Scrivito.setVisitorIdToken(idToken);
} else {
window.localStorage.setItem("loggedInWithGoogle", "yes");
window.location.replace(window.location.href.replace(/404\?path=\%2F/, ""));
}
}
function clearGoogleVisitor() {
window.localStorage.removeItem("loggedInWithGoogle");
if (visitorLoggedInWithGoogle) {
window.location.reload();
}
}
export function LoginWithGoogle() {
return (
<GoogleAPI
clientId={CLIENT_ID}
onUpdateSigninStatus={(isSignedIn) => {
if (isSignedIn) {
setGoogleVisitor({ idToken: googleGetAuthResponse().id_token });
} else {
clearGoogleVisitor();
}
}}
onInitFailure={clearGoogleVisitor}
>
{visitorLoggedInWithGoogle ? (
<GoogleLogout text="Log out" width="160px" />
) : (
<GoogleLogin text="Log in" width="140px" />
)}
</GoogleAPI>
);
}