I am setting JWT authentication (set it up success...
# sst
m
I am setting JWT authentication (set it up successfully). Since i am targeting mobile app, i am trying to think about ways to reduce network IO. One of the thing i want to do that as part of
sign-in
request, I want to add some basic info for user so that their home screen can be rendered. was thinking to return this info as part of sign in request and inject it using some cognito trigger. I setup these triggers
Copy code
const auth = new sst.Auth(this, "Auth", {
  cognito: {
    userPool: userPool,
    userPoolClient: userPoolclient,
    triggers: {
      postAuthentication: "src/lambdas/cognito.postAuthentication",
      preTokenGeneration: "src/lambdas/cognito.preTokenGeneration",
      preAuthentication: "src/lambdas/cognito.preAuthentication",
    },
 },
});
and when i do
aws cognito-idp sign-up
->
aws cognito-idp admin-confirm-sign-up
->
aws cognito-idp initiate-auth
... none of these triggers get triggered. The code for trigger look like this
Copy code
export const preTokenGeneration: APIGatewayProxyHandlerV2 = async (event) => {
  console.log(event);
  return {
    statusCode: 200,
    headers: { "Content-Type": "text/plain" },
    body: `This is from preTokenGeneration ${event.requestContext.time}.`,
  };
};
s
I’m not sure if that is the issue with missing call but I would start with checking typings from aws-lambda/trigger/cognito-user-pool-trigger
i.e. instead using APIGatewayProxyHandlerV2
smth like:
Copy code
import {
  PostAuthenticationTriggerEvent,
  PostAuthenticationTriggerHandler
} from 'aws-lambda/trigger/cognito-user-pool-trigger/post-authentication'

export const handler: PostAuthenticationTriggerHandler = async (
  event: PostAuthenticationTriggerEvent
) => {
  await console.log(event)

  return event
}
for post auth trigger handler
m
okay, will test it. do i have to change the way i am binding them i.e., in
triggers
section?
s
hmm I was using it this way:
Copy code
const postAuthenticationLambda = new Function(this, 'PostHandlerLambda', {
  handler: 'src/postAuthentication.handler'
})

props.userPool = new UserPool(this, 'TestUserPool', {
  userPoolName: 'TestUserPool',
  signInAliases: { email: true, phone: false, username: true },
  selfSignUpEnabled: true,
  removalPolicy: RemovalPolicy.DESTROY,
  lambdaTriggers: {
    postAuthentication: postAuthenticationLambda,
    preSignUp: preSignupLambda
  },
  standardAttributes: {
    email: {
      required: true,
      mutable: true
    }
  }
})
but I’m using @aws-cdk/aws-cognito here
not sst.Auth construct
m
okay, so i got it working. i had to add triggers as part of
userPool
and not the
sst.Auth
function. Here is example
Copy code
const userPool = new cognito.UserPool(this, 'UserPool', {
      selfSignUpEnabled: true,
      signInAliases: {email: true},
      signInCaseSensitive: false,
      lambdaTriggers:{
        postAuthentication: new sst.Function(this, 'PostAuthentication', {
          handler: "src/lambdas/cognito.postAuthentication"
        }),
        ...
     });
I also changed my triggers to what you suggested
f
Hey @Muhammad Ali, in ur original post
Copy code
const auth = new sst.Auth(this, "Auth", {
  cognito: {
    userPool: userPool,
Where is
userPool
coming from?