sst / ion

❍ — a new engine for SST
https://ion.sst.dev
MIT License
1.08k stars 126 forks source link

Add support for linking CognitoUserPoolClients #461

Closed benkraus closed 1 month ago

benkraus commented 1 month ago

When using Cognito, most of the APIs require UserPoolId and/or ClientId. User pools link up just fine with resources, however the CognitoUserPoolClient does not.

One such example is the ConfirmSignUpCommand from @aws-sdk/client-cognito-identity-provider, which requires a ClientId. Linking just doesn't seem to work for the client - no resource is generated in the types.generated.ts file for the client; only the user pool.

Example sst config:

const userPool = new sst.aws.CognitoUserPool("UserPool", {
    usernames: ["email"],
});
const client = userPool.addClient("Client", {
  transform: {
    client: (client) => {
      client.explicitAuthFlows = [
        "ALLOW_REFRESH_TOKEN_AUTH",
        "ALLOW_USER_SRP_AUTH",
        "ALLOW_CUSTOM_AUTH",
        "ALLOW_ADMIN_USER_PASSWORD_AUTH",
      ];
    },
  },
});

const hono = new sst.aws.Function("Hono", {
    url: true,
    handler: "functions/api/index.handler",
    link: [userPool, client],
    environment: {
        COGNITO_CLIENT_ID: client.id,
    },
});

In the above case, the Resource types look like this:

/* tslint:disable */
/* eslint-disable */
import "sst"
declare module "sst" {
  export interface Resource {
    Hono: {
      name: string
      type: "sst.aws.Function"
      url: string
    }
    UserPool: {
      id: string
      type: "sst.aws.CognitoUserPool"
    }
  }
}

export {}

You might notice that in order to work around this, I had to pipe the client id into the environment of the lambda function to access through process.env rather than Resource.

jayair commented 1 month ago

Is this related? https://github.com/sst/ion/pull/460/files

benkraus commented 1 month ago

Hey! I dunno who did that, but that looks like that might solve this!