amplify-js
amplify-js copied to clipboard
authenticator: add key types to user session payload
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.
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
@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 Yes, these types should be updated in the JS repo. Transferring this ticket over there.
We have published an RFC on our plan for improving TypeScript support in Amplify JS & would love to get your feedback & suggestions!