Skip to content

JwtPayload type from aws-amplify/auth is missing cognito:groups attribute #13021

@NicoPowers

Description

@NicoPowers

Before opening, please confirm:

JavaScript Framework

React

Amplify APIs

Authentication

Amplify Version

v6

Amplify Categories

auth

Backend

CDK

Environment information

Details
# Put output below this line
 System:
    OS: Linux 5.15 Ubuntu 22.04.3 LTS 22.04.3 LTS (Jammy Jellyfish)
    CPU: (32) x64 13th Gen Intel(R) Core(TM) i9-13900K
    Memory: 12.47 GB / 15.50 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 20.9.0 - ~/.nvm/versions/node/v20.9.0/bin/node
    Yarn: 1.22.21 - ~/.nvm/versions/node/v20.9.0/bin/yarn
    npm: 10.1.0 - ~/.nvm/versions/node/v20.9.0/bin/npm
    pnpm: 8.15.1 - ~/.nvm/versions/node/v20.9.0/bin/pnpm
  npmPackages:
    @tsconfig/node18: ^18.2.2 => 18.2.2 
    aws-cdk-lib: 2.124.0 => 2.124.0 
    constructs: 10.3.0 => 10.3.0 
    sst: ^2.40.3 => 2.40.3 
    typescript: ^5.3.3 => 5.3.3 
  npmGlobalPackages:
    corepack: 0.20.0
    npm: 10.1.0

Describe the bug

When using fetchAuthSession to retrieve the JWT payload of a signed in user, i can get the payload by doing:

fetchAuthSession().then((session): void => {
      const token = session?.tokens?.idToken;      
      if (!token) {
        console.log("No token found");
        return;
      }
      const payload = token.payload;

However, all of my users will be assigned to some cognito user group, so I would expect the type of payload to contain cognito:groups, but it does not:

interface JwtPayloadStandardFields {
    exp?: number;
    iss?: string;
    aud?: string | string[];
    nbf?: number;
    iat?: number;
    scope?: string;
    jti?: string;
    sub?: string;
   cognito:groups?: string[]; <--- THIS IS MISSING
}

When i console log my payload it shows this:
image

And in fact, there is no scope as well, why is that?

Expected behavior

The types for JwtPayloadStandardFields should contain cognito:groups, especially since the authorized user is being fetched by cognito which inherently supports groups.

Reproduction steps

  1. Start up a basic amplify app with auth
  2. Add custom user pool groups like Admin or SuperUser
  3. Attach the Authenticator HOC to the root of the app
  4. Create a user and assign a cognito user pool group to that user
  5. Fetch and console log the idToken payload as so:
import { fetchAuthSession} from "aws-amplify/auth";
...

useEffect(() => {
    fetchAuthSession().then((session): void => {
      const token = session?.tokens?.idToken;      
      if (!token) {
        console.log("No token found");
        return;
      }
      const payload = token.payload;
      if (!payload) {
        console.log("No payload found");
        return;
      }
      console.log("Payload found", payload); <--- shows cognito:groups but types does not contain that
    });
  }, []);

Code Snippet

// Put your code below this line.

Log output

Details
// Put your logs below this line


aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

Metadata

Metadata

Assignees

Labels

AuthRelated to Auth components/categoryquestionGeneral question

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions