aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.4k stars 2.11k forks source link

authenticator: add key types to user session payload #10142

Open Forfold opened 1 year ago

Forfold commented 1 year ago

On which framework/platform would you like to see this feature implemented?

React

Which UI component is this feature-request for?

Authenticator

Please describe your feature-request in detail.

When using Authenticator with Typescript, I'm seeing the following typing:

/** Known cognito user attributes */
export interface CognitoAttributes {
    email: string;
    phone_number: string;
    [key: string]: string;
}

/** Cognito User Interface */
export interface CognitoUserAmplify extends CognitoUser {
    username?: string;
    attributes?: CognitoAttributes;
}

However, when I ask for the user object from Authenticator I'm seeing many more fields not present on the type- especially the payload that I need to use to determine if a user is an admin or not:

// payload missing on returned user type
interface User {
  signInUserSession?: {
    accessToken: {
      payload: {
        'cognito:groups': string[]
      }
    }
  }
}

...

<Authenticator>
  {({ signOut, user }) => {
    const u: User = user as User
    const payload = u.signInUserSession?.accessToken?.payload
    const groups = payload ? payload['cognito:groups'] : []

...

Please describe a solution you'd like.

Would it be possible to add more a complete typing for the Authenticator user object?

I only found out I needed to use the path u.signInUserSession.accessToken.payload['cognito:groups'] to grab the user groups from this issue https://github.com/aws-amplify/amplify-js/issues/4306

We love contributors! Is this something you'd be interested in working on?

Forfold commented 1 year ago

It looks like if I do an undefined check beforehand I get the typings to do the following

const payload = user?.getSignInUserSession()?.getAccessToken().payload
const groups = payload ? payload['cognito:groups'] : []

though it looks like the keys on payload are still set to any from within amazon-cognito-identity-js

Forfold commented 1 year ago

@reesscot would this issue be better suited for the amazon-cognito-identity-js repo, since that's where the CognitoUser interface is located (what CognitoUserAmplify extends)?

reesscot commented 1 year ago

@Forfold Yes, these types should be updated in the JS repo. Transferring this ticket over there.

jimblanc commented 1 year ago

We have published an RFC on our plan for improving TypeScript support in Amplify JS & would love to get your feedback & suggestions!

RFC: Amplify JS TypeScript Improvements