Skip to content

Instantly share code, notes, and snippets.

@coyksdev
Last active November 7, 2021 07:10
Show Gist options
  • Save coyksdev/7883162190c2fd82782841abbc093b33 to your computer and use it in GitHub Desktop.
Save coyksdev/7883162190c2fd82782841abbc093b33 to your computer and use it in GitHub Desktop.
sst cognito custom domain
import * as cdk from '@aws-cdk/core';
import * as cognito from '@aws-cdk/aws-cognito';
import * as route53 from '@aws-cdk/aws-route53';
import * as route53Targets from '@aws-cdk/aws-route53-targets';
import * as acm from '@aws-cdk/aws-certificatemanager';
import { RemovalPolicy } from "@aws-cdk/core";
import { Auth } from '@serverless-stack/resources';
type AuthConstructProps = cdk.StackProps & {
hostedZoneRootRecord: route53.ARecord;
hostedZone: route53.IHostedZone;
certificate: acm.ICertificate;
stage: string
}
export default class AuthConstruct extends cdk.Construct {
public readonly auth: Auth;
constructor(scope: cdk.Construct, id: string, props: AuthConstructProps) {
super(scope, id);
const prefix = props.stage === 'prod' ? "auth" : props.stage + "-auth";
const domainName = prefix + "." + props.hostedZone.zoneName;
const userPool = new cognito.UserPool(this, "UserPool", {
userPoolName: "UserPool",
signInAliases: { email: true },
selfSignUpEnabled: true,
accountRecovery: cognito.AccountRecovery.EMAIL_ONLY,
signInCaseSensitive: false,
removalPolicy: RemovalPolicy.DESTROY
});
const userPoolClient = new cognito.UserPoolClient(this, "UserPoolClient", {
userPool: userPool,
authFlows: {
userPassword: true,
userSrp: true,
},
});
const userPoolDomain = new cognito.UserPoolDomain(this, "UserPoolDomain", {
userPool: userPool,
customDomain: {
domainName: domainName,
certificate: props.certificate
}
});
userPoolDomain.node.addDependency(props.hostedZoneRootRecord);
new route53.ARecord(this, "UserPoolDomainARecord", {
zone: props.hostedZone,
recordName: domainName,
target: route53.RecordTarget.fromAlias(
new route53Targets.UserPoolDomainTarget(userPoolDomain)
),
});
this.auth = new Auth(this, "Auth", {
cognito: {
userPool,
userPoolClient
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment