Do you guys have suggestions for using cognito tri...
# help
i
Do you guys have suggestions for using cognito triggers? Thinking about applying some attribute changes for different user types (regular/admin etc).
f
You can do something like this:
Copy code
// Create the Lambda function
const authChallengeFn = new sst.Function(this, 'AuthChallengeFn', {
  handler: 'src/authChallenge.main',
});

// Create the User Pool
const userPool = new cognito.UserPool(this, 'MyUserPool', {
  lambdaTriggers: {
    createAuthChallenge: authChallengeFn,
  }
});
Whenever you see
lambda.Function
, create an
sst.Function
instead.
i
Looks like what I wanted, thanks @Frank ! So this would make a cognito.UserPool instead of using sst.Auth then, I'm guessing there would be some additional steps after?
f
Are you currently using
sst.Auth
to protect ur api?
i
Yah:
// Create auth provider
const auth = new sst.Auth(this, 'Auth', {
cognito: {
signInAliases: { email: true },
},
});
auth.attachPermissionsForAuthUsers([api]);
f
Ah so instead of creating a new user pool, use the one created by
sst.Auth
, so something like this:
Copy code
auth.cognitoUserPool.addTrigger(cognito.UserPoolOperation.CREATE_AUTH_CHALLENGE, authChallengeFn);
i
Whoa, exactly what I wanted! I suggest adding that to the docs 😄 Thanks a ton!
f
yeah, good point will do
i
Hey Frank, in this example where are you getting the reference for
cognito
? (cognito.UserPoolOperation)
f
oh right.. you need to import
import * as cognito from "@aws-cdk/aws-cognito";
and run
npx sst add-cdk @aws-cdk/aws-cognito
to add it to ur package.json
i
🙏
f
Hey @Ilia Reingold the
sst.Auth
construct now supports UserPool triggers out of the box in v0.18.0:
Copy code
new Auth(this, "Auth", {
  cognito: {
    triggers: {
      preAuthentication: "src/preAuthentication.main",
      postAuthentication: "src/postAuthentication.main",
    },
  },
});
More examples here - https://docs.serverless-stack.com/constructs/Auth#configuring-userpool-triggers