एडब्ल्यूएस Cognito: उत्पन्न टोकन और बाद में यह ताज़ा के साथ अमेज़न cognito-पहचान-जे एस एसडीके

0

सवाल

मैं लागू कर रहा हूँ एक node.js बैकएंड का उपयोग कर अमेज़न cognito-पहचान-जे एस.

मैं चाहता हूँ बनाने के लिए एक लॉगिन(उपयोगकर्ता नाम, पासवर्ड) और refreshToken(टोकन) APIs.

यह मेरा कोड:

import { AuthenticationDetails, CognitoUser, CognitoUserPool, CognitoRefreshToken } from "amazon-cognito-identity-js"


   
public loginWithAmazonCognitoIdentity (username: string, password: string){
        
        var authenticationData = {
        Username : username,
        Password : password,
    };
    var authenticationDetails = new AuthenticationDetails(authenticationData);
    var poolData = { UserPoolId : 'eu-north-1_xxxxxx',
        ClientId : '3al0l3mhcxxxxxqgnp789987'
    };
    var userPool = new CognitoUserPool(poolData);
    var userData = {
        Username : username,
        Pool : userPool
    };
    var cognitoUser = new CognitoUser(userData);
    const user = cognitoUser.authenticateUser(authenticationDetails, {
        onSuccess: function (result) {
            var accessToken = result.getAccessToken().getJwtToken();
            console.log("token: " + accessToken);
            var refresh = result.getRefreshToken().getToken();
            console.log("RefreshToken: " + refresh);
        },

        onFailure: function(err) {
            console.error(err);
        },

    });

}

इस समारोह रिटर्न एक accessToken और एक refreshToken त्रुटियों के बिना.

इस के बाद, मैं लागू की है, इस समारोह में:

public refreshToken(refreshToken)
    var poolData = { UserPoolId : 'eu-north-1_xxxxxx',
            ClientId : '3al0l3mhcxxxxxqgnp789987'
            };
            var userPool = new CognitoUserPool(poolData);
    
            var userData = {
                Username : 'lacucudi',
                Pool : userPool
            };
            var cognitoUser  = new  CognitoUser(userData);
            var token = new CognitoRefreshToken({ RefreshToken: refreshToken })
            cognitoUser.refreshSession(token, (err, session) => { if (err) {console.log(err)} else console.log('session: ' + JSON.stringify(session)) });
}

लेकिन गुजर refreshToken पहले से लिया गया है यह एक:

NotAuthorizedException: अमान्य टोकन ताज़ा.

कर सकते हैं किसी को मुझे बताओ कि क्या है, सही बैकएंड के कार्यान्वयन के इन 2 एपीआई ?

1

सबसे अच्छा जवाब

0

मैं इस तरह से हल:

    import Amplify, { Auth } from "aws-amplify";
    import {
        AdminCreateUserCommand,
        AdminSetUserPasswordCommand,
        AuthFlowType,
        CognitoIdentityProviderClient,
        CognitoIdentityProviderClientConfig,
        GetUserCommand,
        InitiateAuthCommand,
        MessageActionType,
        RevokeTokenCommand,
    } from "@aws-sdk/client-cognito-identity-provider";

    public async login(username: string, password: string): Promise<AuthTokens> {
            if (!username || !password) {
                throw new HttpException(400, "Please provide both username and password");
            }
    
            Amplify.configure({ Auth: config.auth });
    
            const user = await Auth.signIn(username, password);
    
            if (!user.signInUserSession) {
                throw new HttpException(500, `Could not authenticate user ${username}`);
            }
    
            const {
                signInUserSession: {
                    accessToken: { jwtToken: access_token },
                    idToken: { jwtToken: id_token },
                    refreshToken: { token: refresh_token },
                },
            } = user;
    
            return {
                id_token,
                access_token,
                refresh_token,
            };
        }


public async refresh(refresh_token: string): Promise<AuthTokens> {
        if (!refresh_token) {
            throw new HttpException(400, "Please provide a refresh token");
        }

        const refreshTokenAuth = new InitiateAuthCommand({
            ClientId: config.auth.userPoolWebClientId,
            AuthFlow: AuthFlowType.REFRESH_TOKEN_AUTH,
            AuthParameters: {
                REFRESH_TOKEN: refresh_token,
            },
        });
        const response = await this.client.send(refreshTokenAuth);

        const {
            AuthenticationResult: { AccessToken, IdToken },
        } = response;

        return {
            refresh_token,
            access_token: AccessToken,
            id_token: IdToken,
        };
    }


public async logout(refreshToken: string): Promise<boolean> {
        if (!refreshToken) {
            throw new HttpException(400, "Please provide a refresh token");
        }
        try {
            const command = new RevokeTokenCommand({
                ClientId: config.auth.userPoolWebClientId,
                Token: refreshToken,
            });
            const response = await this.client.send(command);
            const { httpStatusCode } = response.$metadata;
            return httpStatusCode == 200 ?? true;
        } catch (e) {
            logger.error(e);
            throw new HttpException(500, e);
        }
    }

मैं एडब्ल्यूएस-बढ़ाना लॉगिन करने के लिए और एडब्ल्यूएस-एसडीके/ग्राहक-cognito-पहचान प्रदाता के लिए अन्य कार्यों ।

NotAuthorizedException: अमान्य टोकन ताज़ा

त्रुटि संदेश वापस आ गया था क्योंकि डिवाइस पर नज़र रखने के विकल्प सक्षम किया गया था में Cognito सेटिंग्स.

यह अविश्वसनीय के लिए एक सेवा के द्वारा प्रदान की जाती एडब्ल्यूएस देने के लिए गलत त्रुटि संदेश और बहुत कम प्रलेखन के बारे में यह

2021-12-02 15:36:38

अन्य भाषाओं में

यह पृष्ठ अन्य भाषाओं में है

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

इस श्रेणी में लोकप्रिय

लोकप्रिय सवाल इस श्रेणी में