amplify-js icon indicating copy to clipboard operation
amplify-js copied to clipboard

authenticator: add key types to user session payload

Open Forfold opened this issue 2 years ago • 3 comments

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?

  • [ ] 👋 I may be able to implement this feature request.
  • [ ] ⚠️ This feature might incur a breaking change.

Forfold avatar Jul 30 '22 15:07 Forfold

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 avatar Jul 30 '22 21:07 Forfold

@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)?

Forfold avatar Aug 01 '22 23:08 Forfold

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

reesscot avatar Aug 02 '22 00:08 reesscot

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

jimblanc avatar Mar 23 '23 15:03 jimblanc