I'm relatively new to AWS, and so my lack of knowledge of this may be the reason why I don't understand why this doesn't work. However, I've looked around the web as well as the docs for solutions (for a couple of days now); and those solutions, for reasons I still don't understand, do not work in my case.
The problem I'm having is that my users have these custom attributes set to them that aren't present in the jwt access_token when authenticating a user:
These are the custom attributes I need in the token.
However, when authenticating the user on my express backend using the @aws-sdk/client-cognito-identity-provider
:
const pool = await this._awsCognitoService
.initiateAuth({
AuthFlow: "USER_PASSWORD_AUTH",
ClientId: process.env.CLIENT_ID,
AuthParameters: {
USERNAME: data.email,
PASSWORD: data.password,
},
});
after decoding the AccessToken, none of my custom attributes are present.
So I've added a lambda function trigger for pre-token generation (https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-pre-token-generation.html)
and the code looks like this:
exports.handler = (event, context, callback) => {
event.response = {
"claimsOverrideDetails": {
"claimsToAddOrOverride": {
"custom:branch_id": event.request.userAttributes["custom:branch_id"],
"custom:company_id": event.request.userAttributes["custom:company_id"],
},
},
}
callback(null, event)
};
The Pre-Token Generation Lambda Function does get triggered when user authenticates (via express app).
However, the access token retrieved from initiateAuth(...)
has none of those attributes that I've set to override.
I've already set the attributes for the custom attributes on the read and write, so that wasn't the problem. (https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-attributes.html)
I know this had nothing to do with the problem but I was left with no option but to just try. I've enabled all the attributes on App Client Settings just so I can see those sweet sweet custom attributes, but still the same. No custom attribute present on token.
Initially, all those checkboxes were unchecked. I reverted it to it's initial state because this did nothing to help the situation.
Any help would be appreciated