Cognito User Pool: How to refresh Access Token using Refresh Token

前端 未结 7 1414
星月不相逢
星月不相逢 2020-12-12 23:23

I am using Cognito user pool to authenticate users in my system. A successful authentication gives an ID Token (JWT), Access Token (JWT) and a Refresh

相关标签:
7条回答
  • 2020-12-13 00:28

    Using NodeJS aws-sdk and a bit of Promise you can await authentication using Refresh Token with initiateAuth as follows:

    const {CognitoIdentityServiceProvider} = require('aws-sdk');
    
    const initiateAuth = (ClientId, REFRESH_TOKEN, DEVICE_KEY) =>
        new Promise((resolve, reject) => {
            const CISP = new CognitoIdentityServiceProvider();
    
            CISP.initiateAuth(
                {
                    ClientId, // Cognito App Client Id
                    AuthFlow: 'REFRESH_TOKEN_AUTH',
                    AuthParameters: {
                        REFRESH_TOKEN,
                        DEVICE_KEY
                    }
                },
                (err, data) => {
                    if (err) {
                        return reject(err);
                    }
    
                    resolve(data);
                }
            );
        });
    
    // ------ Usage ------ //
    
    (async () => {
        const tokens = await initiateAuth('mY4pps3cR3T', '<R3FR3SHT0K3N>');
    
        console.log('Tokens', tokens);
    
        const {AuthenticationResult: {AccessToken, IdToken, ExpiresIn, TokenType}} = tokens;
    })()
    

    Keep in mind that if Device tracking is enabled you should pass a device key otherwise you can receive Invalid refresh token error.

    0 讨论(0)
提交回复
热议问题