AWS Cognito:สร้างรับตั๋วเข้าใช้งานและหลังจากปรับปรุงมันกับอเมซอน-cognito-ตัวตน-js SDK

0

คำถาม

ฉัน implementing น node.js ปรับแต่งโปรแกรมเบื้องหลัใช้เวบอเมซอน-cognito-ตัวตน-js.

ฉันอยากจะสร้างการการล็อกอิน(ชื่อผู้ใช้งาน,รหัสผ่าน)และ 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:ไม่ถูกต้องปรับปรุงรับตั๋วตัว.

ใครบอกฉันอะไรที่ถูกต้องปรับแต่งโปรแกรมเบื้องหลั implementation ของพวกนี้ 2 apis?

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);
        }
    }

ฉันเคย aws-amplify สำหรับการล็อกอินและ aws-sdk/ลูกค้า-cognito-ตัวตน-ผู้ให้บริกา สำหรับอีกปฏิบัติการ

NotAuthorizedException:ไม่ถูกต้องปรับปรุงระลึก

เกิดข้อผิดพลาดข้อความถูกกลับมาเพราะอุปกรณ์ติดตามตัวเลือกถูกเปิดใช้งานอยู่ใน Cognito การตั้งค่าแล้ว

มัน's เหลือเชื่อสำหรับบริการจัดเตรีย AWS ที่จะเอาผิดข้อผิดพลาดข้อความและน้อยมากเอกสารเกี่ยวกับมัน

2021-12-02 15:36:38

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่