Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Identity Services: initCodeClient().requestCode() prompts user signin even though token is valid

After obtaining authorization token:

    const tokenClient = google.accounts.oauth2.initTokenClient({
       client_id, scope, callback, 
    });
            
    // ...
    
    tokenClient.requestAccessToken({ prompt: ''})

I need to obtain authorization code and send it to backend:

    let code;

    const codeClient = google.accounts.oauth2.initCodeClient({
        scope, client_id, 
        callback: codeResponse => code = codeResponse.code,
    });

    codeClient.requestCode();

Despite the user has authorized once, and gapi.client.getToken() is not null the popup window appears another time.

Is there any solution to avoid redundant user authorization?

like image 477
Alexander Avatar asked May 06 '26 16:05

Alexander


1 Answers

Most likely it's because user's consent on obtaining token (initTokenClient) does not automatically mean consent on obtaining authorization code (initCodeClient). You can obtain authorization code and translate it to a token in backend which could be then used in frontend as gapi.client.setToken(token) if the scope matches.

like image 159
Ihor Ivanets Avatar answered May 11 '26 15:05

Ihor Ivanets



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!