Artem Kalantai
05/22/2021, 7:24 AMconst userPool = cognito.UserPool.fromUserPoolId(stack, "users", "us-east-1_iYNNUVj0l");
Ross Coundon
05/22/2021, 7:32 AMArtem Kalantai
05/22/2021, 7:32 AMconst auth = new Auth(stack, "Auth", {
cognito: {
userPool: userPool,
userPoolClient: userPoolClient,
},
google: {
clientId:
"id",
},
});
Ross Coundon
05/22/2021, 7:38 AMArtem Kalantai
05/22/2021, 7:39 AMFrank
Artem Kalantai
05/22/2021, 7:40 AMRoss Coundon
05/22/2021, 7:43 AMreadonly userPool?: cognito.UserPoolProps | cognito.IUserPool;
The error goes awayFrank
readonly cognitoUserPool?: cognito.IUserPool;
and in v0.12.0, we now have
readonly userPool?: cognito.UserPoolProps | cognito.UserPool;
Frank
IUserPool
, not it takes doesn’t ie. UserPool
Ross Coundon
05/22/2021, 7:47 AMFrank
IUserPool
should’ve never worked before.Frank
Ross Coundon
05/22/2021, 7:52 AMFrank
Frank
Auth
construct creates a Cognito Identity Pool, and optionally a Cognito User Pool if providedFrank
userPool.userPoolProviderName
https://github.com/serverless-stack/serverless-stack/blob/master/packages/resources/src/Auth.ts#L202Frank
UserPool
has it, but imported IUserPool
doesn’t have that property.Artem Kalantai
05/22/2021, 7:56 AMFrank
Artem Kalantai
05/22/2021, 7:56 AMFrank
Artem Kalantai
05/22/2021, 7:57 AMArtem Kalantai
05/22/2021, 7:58 AMFrank
Artem Kalantai
05/22/2021, 8:00 AMFrank
Frank
Artem Kalantai
05/22/2021, 8:02 AMimport * 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};
}
Artem Kalantai
05/22/2021, 8:03 AMArtem Kalantai
05/22/2021, 8:04 AMimport * 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};
}
Artem Kalantai
05/22/2021, 8:04 AMdefaultAuthorizationType: sst.ApiAuthorizationType.NONE,
this part is temp, it will be JWT laterFrank
Auth
constructArtem Kalantai
05/22/2021, 8:05 AMFrank
Artem Kalantai
05/22/2021, 8:05 AMArtem Kalantai
05/22/2021, 8:05 AMArtem Kalantai
05/22/2021, 8:05 AMArtem Kalantai
05/22/2021, 8:07 AMArtem Kalantai
05/22/2021, 8:07 AMArtem Kalantai
05/22/2021, 8:07 AM`if (auth.cognitoUserPool) {
auth.cognitoUserPool.addTrigger(cognito.UserPoolOperation.POST_CONFIRMATION, authChallengeFn);
}
Artem Kalantai
05/22/2021, 8:07 AMFrank
Frank
Artem Kalantai
05/22/2021, 8:07 AMFrank
Frank
Auth
construct, and replace
if (auth.cognitoUserPool) {
auth.cognitoUserPool.addTrigger(cognito.UserPoolOperation.POST_CONFIRMATION, authChallengeFn);
}
with
userPool.addTrigger(cognito.UserPoolOperation.POST_CONFIRMATION, authChallengeFn);
Frank
Artem Kalantai
05/22/2021, 8:12 AMArtem Kalantai
05/22/2021, 8:12 AMArtem Kalantai
05/22/2021, 8:12 AMFrank
Frank
Artem Kalantai
05/22/2021, 8:13 AMFrank
Artem Kalantai
05/22/2021, 8:17 AMArtem Kalantai
05/22/2021, 8:17 AMFrank
Frank
Artem Kalantai
05/23/2021, 5:49 PM