infomofo
infomofo

Reputation: 1685

How do I log out of a chrome.identity oauth provider

I'm using chrome.identity to log into a 3rd party oauth provider in an chrome extension. It works fine for logging in- when I use launchWebAuthFlow I am presented with the third party login screen and redirected back to my application after the signin flow.

However, I can't find a way to enable log out functionality in my extension. There doesn't seem to be a function to clear the cached logged in identity. The next time that launchWebAuthFlow is called, it will automatically log me in as the first user, and not prompt me to log in again.

Is there any way to clear the logged in state of the chrome.identity plugin?

Upvotes: 23

Views: 8344

Answers (8)

hypers
hypers

Reputation: 1250

If you try launchWebAuthFlow to logout but get User interaction required error, then you need to add one more flag along with the url:

chrome.identity.launchWebAuthFlow (
  {'url': 'https://some-logout-url/',
   'interactive': true },
   function(result) {
     console.log(result);
   }
);

Upvotes: -2

Vladyslav Babenko
Vladyslav Babenko

Reputation: 1369

I could achieve result only with this implementation

  chrome.identity.getAuthToken({ 'interactive': false }, currentToken => {
    if (!chrome.runtime.lastError) {
      // Remove the local cached token
      chrome.identity.removeCachedAuthToken({ token: currentToken }, () => {})

      // Make a request to revoke token in the server
      const xhr = new XMLHttpRequest()
      xhr.open('GET', `${googleRevokeApi}${currentToken}`)
      xhr.send()

      // Update the user interface accordingly
      // TODO: your callback
    }
  })

Upvotes: 0

user5858
user5858

Reputation: 1221

I've found that calling these two in the sequence is working:

var url = 'https://accounts.google.com/o/oauth2/revoke?token=' + token;
window.fetch(url);

chrome.identity.removeCachedAuthToken({token: token}, function (){
  alert('removed');
});

Upvotes: 17

Kyaw Tun
Kyaw Tun

Reputation: 13141

For me, https://accounts.google.com/logout does not work. But https://accounts.google.com/o/oauth2/revoke?token=TOKEN work well, using simple window.fetch(url), not with hrome.identity.launchWebAuthFlow.

Upvotes: 3

Lê Vinh
Lê Vinh

Reputation: 61

You should add prompt=select_account to your auth URL. Your problem will be solved.

https://accounts.google.com/o/oauth2/auth?client_id={clientId}&response_type=token&scope={scopes}&redirect_uri={redirectURL}&prompt=select_account

Upvotes: 5

Walty Yeung
Walty Yeung

Reputation: 3562

I happened to hit the same problem recently, and I finally solved it by adding login_hint=<new_user> and prompt=consent in the login URL.

Upvotes: 0

mlfan
mlfan

Reputation: 381

I am not aware about the specific third party provider. But I faced the similar problem when using Google Oauth with chrome.identity.launchWebAuthFlow(). I could sign in the user, but not sign out using removeCachedAuthToken()

In this case, to logout the user, I used chrome.identity.launchWebAuthFlow() with Google's logout URL rather than it's oauth URL

chrome.identity.launchWebAuthFlow(
    { 'url': 'https://accounts.google.com/logout' },
    function(tokenUrl) {
        responseCallback();
    }
);

This worked pretty well.

Upvotes: 22

user235273
user235273

Reputation:

You can clear the identity cache using the chrome.identity.removeCachedAuthToken(object details, function callback) method.
https://developer.chrome.com/apps/identity#method-removeCachedAuthToken

Upvotes: 0

Related Questions