Search code examples
typescriptaws-amplify

Property 'idToken' does not exist on type 'CognitoUserSession'


I am trying to destructure payload from idToken within userData.

userData is of type CognitoUserSession.

import { CognitoUser } from "@aws-amplify/auth";
import { CognitoUserSession, CognitoIdToken } from "amazon-cognito-identity-js";

  const userData = await Auth.currentSession().catch(e => console.log(e));
  if (!userData) {
    console.log(`User data : ${userData}`);
    updateUser({} as CognitoUser);
    return;
  }
    const {
    idToken: { payload: CognitoIdToken }
  } : CognitoUserSession = userData;

But I get the error that idToken does not exist on type CognitoUserSession but in its class it exist.

export interface ICognitoUserSessionData {
    IdToken: CognitoIdToken;
    AccessToken: CognitoAccessToken;
    RefreshToken?: CognitoRefreshToken;
}

export class CognitoUserSession {
    constructor(data: ICognitoUserSessionData);

    public getIdToken(): CognitoIdToken;
    public getRefreshToken(): CognitoRefreshToken;
    public getAccessToken(): CognitoAccessToken;
    public isValid(): boolean;
}

export class CognitoAccessToken {
    payload: { [key: string]: any };

    constructor({ AccessToken }: { AccessToken: string });

    public getJwtToken(): string;
    public getExpiration(): number;
    public getIssuedAt(): number;
    public decodePayload(): { [id: string]: any };
}

export class CognitoIdToken {
    payload: { [key: string]: any };

    constructor({ IdToken }: { IdToken: string });

    public getJwtToken(): string;
    public getExpiration(): number;
    public getIssuedAt(): number;
    public decodePayload(): { [id: string]: any };
}

Solution

  • I think the AWS team updated the aws-amplify package. If you want to destructure use the old package aws-amplify. You are using the new package @aws-amplify/auth that now uses the getter function so you can no longer destructure.

    try const { payload } = (await Auth.currentSession()).getIdToken()