Skip to content

Commit ad46a5e

Browse files
committed
feat: add setupCustomDomain function
1 parent a5e20ec commit ad46a5e

File tree

2 files changed

+46
-35
lines changed

2 files changed

+46
-35
lines changed

packages/auth-construct/src/construct.ts

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
AttributeMapping,
4444
AuthProps,
4545
CustomAttribute,
46+
CustomDomainOptions,
4647
CustomSmsSender,
4748
EmailLoginSettings,
4849
ExternalProviderOptions,
@@ -60,10 +61,7 @@ import {
6061
Certificate,
6162
CertificateValidation,
6263
} from 'aws-cdk-lib/aws-certificatemanager';
63-
import {
64-
CloudFrontTarget,
65-
UserPoolDomainTarget,
66-
} from 'aws-cdk-lib/aws-route53-targets';
64+
import { UserPoolDomainTarget } from 'aws-cdk-lib/aws-route53-targets';
6765

6866
type DefaultRoles = { auth: Role; unAuth: Role };
6967
type IdentityProviderSetupResult = {
@@ -869,6 +867,38 @@ export class AmplifyAuth
869867
return undefined;
870868
};
871869

870+
private setupCustomDomain = (
871+
userPool: UserPool,
872+
customDomainOptions: CustomDomainOptions,
873+
) => {
874+
const hostedZone = HostedZone.fromHostedZoneAttributes(
875+
this,
876+
`${this.name}HostedZone`,
877+
customDomainOptions.hostedZone,
878+
);
879+
880+
const certificate = new Certificate(this, `${this.name}Certificate`, {
881+
domainName: customDomainOptions.domainName,
882+
validation: CertificateValidation.fromDns(hostedZone),
883+
});
884+
885+
const customDomain = userPool.addDomain(
886+
`${this.name}UserPoolCustomDomain`,
887+
{
888+
customDomain: {
889+
domainName: customDomainOptions.domainName,
890+
certificate,
891+
},
892+
},
893+
);
894+
895+
new ARecord(this, `${this.name}ARecord`, {
896+
zone: hostedZone,
897+
recordName: customDomainOptions.domainName,
898+
target: RecordTarget.fromAlias(new UserPoolDomainTarget(customDomain)),
899+
});
900+
};
901+
872902
/**
873903
* Setup External Providers (OAuth/OIDC/SAML) and related settings
874904
* such as OAuth settings and User Pool Domains
@@ -1070,37 +1100,10 @@ export class AmplifyAuth
10701100
);
10711101
}
10721102

1073-
const stack = Stack.of(this);
1074-
1075-
const hostedZone = HostedZone.fromHostedZoneAttributes(
1076-
stack,
1077-
'hostedZone',
1078-
{
1079-
hostedZoneId: 'Z00739961UF0WORM8EZIG',
1080-
zoneName: 'goheim.com',
1081-
},
1082-
);
1083-
1084-
const certificate = new Certificate(stack, 'certificate', {
1085-
domainName: 'auth.goheim.com',
1086-
validation: CertificateValidation.fromDns(hostedZone),
1087-
});
1088-
1089-
const customDomain = this.userPool.addDomain(
1090-
`${this.name}UserPoolCustomDomain`,
1091-
{
1092-
customDomain: {
1093-
domainName: 'auth.goheim.com',
1094-
certificate,
1095-
},
1096-
},
1097-
);
1098-
1099-
new ARecord(Stack.of(this), `${this.name}ARecord`, {
1100-
zone: hostedZone,
1101-
recordName: 'auth.goheim.com',
1102-
target: RecordTarget.fromAlias(new UserPoolDomainTarget(customDomain)),
1103-
});
1103+
// Generate a custom domain if custom domain options are specified
1104+
if (external.customDomainOptions) {
1105+
this.setupCustomDomain(this.userPool, external.customDomainOptions);
1106+
}
11041107

11051108
// oauth settings for the UserPool client
11061109
result.oAuthSettings = {

packages/auth-construct/src/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
UserPoolSESOptions,
1111
} from 'aws-cdk-lib/aws-cognito';
1212
import { IFunction } from 'aws-cdk-lib/aws-lambda';
13+
import { HostedZoneAttributes } from 'aws-cdk-lib/aws-route53';
1314
export type VerificationEmailWithLink = {
1415
/**
1516
* The type of verification. Must be one of "CODE" or "LINK".
@@ -269,6 +270,11 @@ export type SamlProviderProps = Omit<
269270
};
270271
} & IdentityProviderProps;
271272

273+
export type CustomDomainOptions = {
274+
hostedZone: HostedZoneAttributes;
275+
domainName: string;
276+
};
277+
272278
/**
273279
* External provider options.
274280
*/
@@ -334,6 +340,8 @@ export type ExternalProviderOptions = {
334340
* List of allowed logout URLs for the identity providers.
335341
*/
336342
logoutUrls: string[];
343+
344+
customDomainOptions?: CustomDomainOptions;
337345
};
338346

339347
/**

0 commit comments

Comments
 (0)