I have been trying to implement google sign in using OAuth 2 in my chrome extension. For doing so I am using the chrome identity API. But the problem is that when I load up the extension first everything works fine.
- I click on login, a pop-up opens asking me to choose my google account
- Then I am asked to give the required permissions
- I am logged in.
But then when I log out and try to login again, the pop-up skips the part where I have to choose my google account, rather it directly skips to the permissions part keeping the same account that I chose the previous time. Here's the code for login and logout which I am using :
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log('listener called')
console.log(request.message)
if (request.message === 'get_auth_token') {
chrome.identity.getAuthToken({ interactive: true }, function (token) {
user_signed_in = true
console.log('token: ' + token)
})
} else if (request.message === 'get_profile') {
chrome.identity.getProfileUserInfo({ accountStatus: 'ANY' }, function (
user_info,
) {
console.log(user_info)
})
} else if (request.message === 'logout_user') {
revokeToken()
}
})
function revokeToken() {
chrome.identity.getAuthToken({ interactive: false }, function (
current_token,
) {
if (!chrome.runtime.lastError) {
// Remove the local cached token
chrome.identity.removeCachedAuthToken(
{ token: current_token },
function () {},
)
// Make a request to revoke token in the server
var xhr = new XMLHttpRequest()
xhr.open(
'GET',
'https://accounts.google.com/o/oauth2/revoke?token=' + current_token,
)
xhr.send()
console.log(
'Token revoked and removed from cache. ' +
'Check chrome://identity-internals to confirm.',
)
alert('You are logged out.')
}
})
}