because I’m getting userPool from existing userPoo...
# help
a
because I’m getting userPool from existing userPool
Copy code
const userPool = cognito.UserPool.fromUserPoolId(stack, "users", "us-east-1_iYNNUVj0l");
r
Can you share the line that causes the error?
a
Copy code
const auth = new Auth(stack, "Auth", {
    cognito: {
      userPool: userPool,
      userPoolClient: userPoolClient,
    },
    google: {
      clientId:
        "id",
    },
  });
r
I just tried and it and get the same issue
a
oo cool, then you can fix it I hope 🙂
f
hey guys, it’s a bit late today. I can definitely take a look at this tmr.
a
sure, np @Frank, thanks a lot
r
Seems to be to do with this. If I change it to
readonly userPool?: cognito.UserPoolProps | cognito.IUserPool;
The error goes away
f
Yeah had a quick look, in v0.11.0, we had
Copy code
readonly cognitoUserPool?: cognito.IUserPool;
and in v0.12.0, we now have
Copy code
readonly userPool?: cognito.UserPoolProps | cognito.UserPool;
So it used to allow imported user pool ie.
IUserPool
, not it takes doesn’t ie.
UserPool
r
Is that deliberate, Frank?
f
Just looked at the doc for cognito again, and yes it was deliberate.
IUserPool
should’ve never worked before.
Let me explain.
r
You can go to bed and explain tomorrow 😄
f
lol.. it should be quick 😂
The
Auth
construct creates a Cognito Identity Pool, and optionally a Cognito User Pool if provided
To hook up the User Pool to the Identity Pool, it needs
userPool.userPoolProviderName
https://github.com/serverless-stack/serverless-stack/blob/master/packages/resources/src/Auth.ts#L202
UserPool
has it, but imported
IUserPool
doesn’t have that property.
a
yea, I see, what can I do with this?
f
So, it should’ve not worked before.
a
probably it wasn’t work
f
Artem, are you trying to create a new Identity Pool and hook it up to your existing User Pool? Can you explain ur use case a bit.
a
no, just want to use existing userpool, so i don’t need to connect identity pool
because it is already there
f
oh I see.. do you have an Api and are you protecting it using IAM?
a
yea
f
hmm.. if ur API is protected using IAM, you might be using an identity pool
can you show me what your CDK code looks like?
a
Copy code
import * as sst from "@serverless-stack/resources";
import * as cognito from "@aws-cdk/aws-cognito";
import {Auth, Permissions} from "@serverless-stack/resources";
import * as cdk from "@aws-cdk/core";

export const configAuth = (stack: sst.Stack, environment: any, permissions: Permissions) => {
  // const userPool = new cognito.UserPool(stack, "UserPool", {
  //   userPoolName: "users",
  //   selfSignUpEnabled: true,
  //   signInAliases: {
  //     email: true
  //   },
  //   standardAttributes: {
  //     nickname: {
  //       required: false,
  //     },
  //   },
  // });

  const userPool = cognito.UserPool.fromUserPoolId(stack, "users", "");


  userPool.addClient("googleClient",
    {
      supportedIdentityProviders: [cognito.UserPoolClientIdentityProvider.GOOGLE, cognito.UserPoolClientIdentityProvider.COGNITO],
      oAuth: {
        flows: {
          authorizationCodeGrant: true,
        },
        scopes: [cognito.OAuthScope.OPENID, cognito.OAuthScope.EMAIL, cognito.OAuthScope.PROFILE],
        callbackUrls: ['<http://localhost:3000/signIn>'],
        logoutUrls: ['<http://localhost:3000>'],
      }
    })

  userPool.registerIdentityProvider(new cognito.UserPoolIdentityProviderGoogle(stack, "googleIdentity", {
    clientId: 'id',
    clientSecret: 'secrret',
    userPool: userPool,
  }))

  const userPoolClient = cognito.UserPoolClient.fromUserPoolClientId(stack, "UserPollClient", "");

  const auth = new Auth(stack, "Auth", {
    cognito: {
      userPool: userPool,
      userPoolClient: userPoolClient,
    },
    google: {
      clientId:
        "id",
    },
  });

  const authChallengeFn = new sst.Function(stack, 'createDynamoUser', {
    handler: 'src/auth/onAuth.createDynamoUser',
    environment: environment,
  });

  if (auth.cognitoUserPool) {
    auth.cognitoUserPool.addTrigger(cognito.UserPoolOperation.POST_CONFIRMATION, authChallengeFn);
  }

  authChallengeFn.attachPermissions(permissions)

  new cdk.CfnOutput(stack, "UserPoolId", {
    value: userPool.userPoolId,
  });
  new cdk.CfnOutput(stack, "UserPoolClientId", {
    value: userPoolClient.userPoolClientId,
  });

  return {userPool, userPoolClient};
}
and then I’m using it in
Copy code
import * as sst from "@serverless-stack/resources";
import {Permissions} from "@serverless-stack/resources";
import * as apigAuthorizers from "@aws-cdk/aws-apigatewayv2-authorizers";
import * as cdk from "@aws-cdk/core";
import {UserPoolAuthorizerProps} from "@aws-cdk/aws-apigatewayv2-authorizers";

export const configApi = (stack: sst.Stack, environment: any, permissions: Permissions, poolPros: UserPoolAuthorizerProps) => {
  const coreApi = new sst.Api(stack, "coreApi", {
    cors: true,
    defaultAuthorizationType: sst.ApiAuthorizationType.NONE,
    defaultAuthorizer: new apigAuthorizers.HttpUserPoolAuthorizer(poolPros),
    routes: {
      "GET /users": {
        function: new sst.Function(stack, "fetchUsers", {
          handler: "src/users/usersApi.listUsers",
          timeout: 300,
          environment: environment,
        }),
      },
      "GET /user/{id}": {
        function: new sst.Function(stack, "fetchSingleUser", {
          handler: "src/users/usersApi.getSingleUser",
          timeout: 300,
          environment: environment,
        }),
      },
      "DELETE /user/{id}": {
        function: new sst.Function(stack, "removeSingleUser", {
          handler: "src/users/usersApi.removeUser",
          timeout: 300,
          environment: environment,
        }),
      },
      "POST /user": {
        function: new sst.Function(stack, "createUser", {
          handler: "src/users/usersApi.createUser",
          timeout: 300,
          environment: environment,
        }),
      },
      "PUT /user/{id}": {
        function: new sst.Function(stack, "updateSingleUser", {
          handler: "src/users/usersApi.updateUser",
          timeout: 300,
          environment: environment,
        }),
      },
      "GET /posts": {
        function: new sst.Function(stack, "fetchPosts", {
          handler: "src/posts/postsApi.listPosts",
          timeout: 300,
          environment: environment,
        }),
      },
      "GET /post/{id}": {
        function: new sst.Function(stack, "fetchSinglePost", {
          handler: "src/posts/postsApi.getSinglePost",
          timeout: 300,
          environment: environment,
        }),
      },
      "DELETE /post/{id}": {
        function: new sst.Function(stack, "removeSinglePost", {
          handler: "src/posts/postsApi.removePost",
          timeout: 300,
          environment: environment,
        }),
      },
      "POST /post": {
        function: new sst.Function(stack, "createPost", {
          handler: "src/posts/postsApi.createPost",
          timeout: 300,
          environment: environment,
        }),
      },
      "PUT /post/{id}": {
        function: new sst.Function(stack, "updateSinglePost", {
          handler: "src/posts/postsApi.updatePost",
          timeout: 300,
          environment: environment,
        }),
      },
      "GET /contents": {
        function: new sst.Function(stack, "fetchAllContent", {
          handler: "src/content/contentApi.listContent",
          timeout: 300,
          environment: environment,
        }),
      },
      "PUT /content/{id}": {
        function: new sst.Function(stack, "updateContent", {
          handler: "src/content/contentApi.updateContent",
          timeout: 300,
          environment: environment,
        }),
      },
      "GET /content/{id}": {
        function: new sst.Function(stack, "getSingleContent", {
          handler: "src/content/contentApi.getSingleContent",
          timeout: 300,
          environment: environment,
        }),
      },
      "POST /content": {
        function: new sst.Function(stack, "createContent", {
          handler: "src/content/contentApi.createContent",
          timeout: 300,
          environment: environment,
        }),
      },
      "POST /contentFromGoogle": {
        function: new sst.Function(stack, "googleDriveContent", {
          handler: "src/contentManager/googleDriveToS3.send",
          timeout: 300,
          environment: environment,
        }),
      },
    },
  });

  coreApi.attachPermissions(permissions);

  new cdk.CfnOutput(stack, "core", {
    value: coreApi.httpApi.apiEndpoint,
  });

  return {coreApi};
}
Copy code
defaultAuthorizationType: sst.ApiAuthorizationType.NONE,
this part is temp, it will be JWT later
f
ah.. i don’t think you are using the
Auth
construct
a
so I don’t need it at all?
f
yeah.. u can try taking it out… it’s probably not doing anything
a
sometimes I confused which construct I need and which not, so, thanks I’ll try
go sleep please
it is too late
hmmm…..
I’m using it here
Copy code
`if (auth.cognitoUserPool) {
    auth.cognitoUserPool.addTrigger(cognito.UserPoolOperation.POST_CONFIRMATION, authChallengeFn);
  }
anyway, we will continue after you sleep
f
Ah, I see.. you are using it to add triggers
Yup, I will follow up on this tmr
a
good night
f
yup.. will continue this thread tmr
In the mean time, you can remove the
Auth
construct, and replace
Copy code
if (auth.cognitoUserPool) {
  auth.cognitoUserPool.addTrigger(cognito.UserPoolOperation.POST_CONFIRMATION, authChallengeFn);
}
with
Copy code
userPool.addTrigger(cognito.UserPoolOperation.POST_CONFIRMATION, authChallengeFn);
I will explain the details tmr
a
thanks
TS2339: Property ‘addTrigger’ does not exist on type ‘IUserPool’.
ok, I’m stoping, go to bed
f
lol yup… I actually need to read up on this tmr
maybe CDK doesn’t let u add triggers after the user pool is created
a
maybe
f
So it is a CDK limitation, try this solution? https://github.com/aws/aws-cdk/issues/10002#issuecomment-772749855
a
GO IN YOUR BED 😆
thanks
f
gl.. chat tmr
@Artem Kalantai were u able to add the user pool triggers?
a
nope, because i did get how to use template from example, i tried another way, but had the issue i send in main thread