Google Cloud Endpoints - Switch User / Request Re-Login

When you authenticate with Google Cloud Endpoints, if there are several Google accounts that have already granted your permission to the application, it simply selects the default account, as Gmail does.

1) Gmail has the ability to switch Google accounts in the upper right corner. How can I achieve something like this?

2) Can you require the user to log back into their Google account, even if they are already logged in? Similar to how the user proceeds to change the settings of his account.

+6
source share
1 answer

I found a way, but a little sad that this function is not included (or not documented) in gapi.auth.authorize .

In any case, if you open the authorization pop-up window manually and then process the received token, you can pass additional parameters, such as prompt=select_account , which will allow the user to select their account.

Here is a sample code. When blocking pop-ups, you will need to call this function in the onclick event so that the pop-up does not block.

Therefore, the code is not ready for production. We do not manage cases, for example, when a user refuses to give his consent, and we do not transmit additional information about tokens, such as the expiration time.

 var switchUserAccount = function (callback) { var popup = window.open("https://accounts.google.com/o/oauth2/auth?client_id=102862643449-geb89aoann7dj6tsha4mtkhvos5mk01b.apps.googleusercontent.com" + "&prompt=select_account" + "&scope=https://www.googleapis.com/auth/userinfo.email" + "&redirect_uri=https://david-sandbox.appspot.com/autoclose.html" + "&access_type=online&response_type=token", "thewindow"); var waitForPopup = function () { try { var token = popup.location.hash.substring(14).split("&")[0]; console.log("FOund token !" + token); if (token == "") { console.log("Not ready yet") setTimeout(waitForPopup, 500); } else { gapi.auth.setToken({access_token: token}); popup.close(); callback(); } } catch (e) { console.log("Not ready yet, exception") setTimeout(waitForPopup, 500); } }; waitForPopup(); } 
+3
source

Source: https://habr.com/ru/post/975858/


All Articles