index.ejs 1.25 KB
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <script src="https://apis.google.com/js/platform.js" async defer></script>
    <meta name="google-signin-client_id" content=<%=d%>>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><%= d%></title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
</head>

<body>
<h1>Login</h1>
<div class="g-signin2" data-onsuccess="onSignIn"></div>
<a href="#" onclick="signOut();">Sign out</a>
</body>


<script>
    function onSignIn(googleUser) {
        var id_token = googleUser.getAuthResponse().id_token;
        //console.log(id_token);
        var xhr = new XMLHttpRequest();
        xhr.open('POST', '/index');
        xhr.setRequestHeader('Content-Type', 'application/json');
        xhr.onload = function() {
            console.log('Signed in as: ' + xhr.responseText);
            if(xhr.responseText == 'success'){
                signOut();
                location.assign('/login')
            }
        };
        xhr.send(JSON.stringify({token: id_token}));
    }

    function signOut() {
        var auth2 = gapi.auth2.getAuthInstance();
        auth2.signOut().then(function () {
            console.log('User signed out.');
        });
    }
</script>
</html>