Сохранить состояние входа в Google - PullRequest
0 голосов
/ 18 апреля 2020

Я новичок ie в API Google. Я пытаюсь встроить кнопку входа в Google на свой сайт. Я пытаюсь избежать использования их встроенной кнопки. Приведенный ниже код работает для входа пользователя, но я не могу понять, как заставить мою веб-страницу помнить, что они вошли в систему, когда пользователь обновляет страницу, перемещается на нескольких страницах веб-сайта или покидает сайт и возвращается.

// Client ID and API key from the Developer Console
var CLIENT_ID = 'MY_CREDENTIALS.apps.googleusercontent.com';
var API_KEY = 'MY_API_KEY';

// Array of API discovery doc URLs for APIs used by the quickstart
var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/drive/v3/rest"];

// Authorization scopes required by the API; multiple scopes can be
// included, separated by spaces.
var SCOPES = 'https://www.googleapis.com/auth/drive';

var authorizeButton = document.getElementById('authorize_button');


/**
 *  On load, called to load the auth2 library and API client library.
 */

function handleClientLoad() {
    gapi.load('client:auth2', initClient);
}


/**
 *  Initializes the API client library and sets up sign-in state
 *  listeners.
 */
function initClient() {
    gapi.client.init({
        apiKey: API_KEY,
        clientId: CLIENT_ID,
        discoveryDocs: DISCOVERY_DOCS,
        scope: SCOPES
    }).then(function() {
        // Listen for sign-in state changes.
        gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);

        // Handle the initial sign-in state.
        updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
        authorizeButton.onclick = handleAuthClick;

    }, function(error) {
        console.log(JSON.stringify(error, null, 2));
    });
}



/**
 *  Called when the signed in status changes, to update the UI
 *  appropriately. After a sign-in, the API is called.
 */
function updateSigninStatus(isSignedIn) {
    if (isSignedIn) {
        authorizeButton.style.display = 'none';
    }
}



/**
 *  Sign in the user upon button click.
 */
function handleAuthClick(event) {
    gapi.auth2.getAuthInstance().signIn();
}



/**
 *  Sign out the user upon button click.
 */
function handleSignoutClick(event) {
    if (window.confirm('Are you sure you want to signout?')) {
        var auth2 = gapi.auth2.getAuthInstance();
        auth2.signOut().then(function() {
            console.log('User signed out.');
        });
        auth2.disconnect();
        sessionStorage.removeItem("myUserEntity");
        window.open('index.php', '_self');
    }
}

Пожалуйста, реализуйте свой код в приведенном выше коде, чтобы я лучше понял, как идут дела. Любая помощь высоко ценится!

...