Skip to content

Commit

Permalink
feat: add support for enabling self signup in Cognito
Browse files Browse the repository at this point in the history
  • Loading branch information
agdimech committed Jun 18, 2024
1 parent cc4f95a commit 300a8db
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ const Auth: React.FC<any> = ({ children }) => {
clientId={runtimeContext.userPoolWebClientId}
region={runtimeContext.region}
identityPoolId={runtimeContext.identityPoolId}
{{#allowSignup}}allowSignup={true}
{{/allowSignup}}signUpAttributes={[
{
displayName: "Email",
name: "email",
required: true,
},
{
displayName: "Given name",
name: "given_name",
required: true,
},
{
displayName: "Last name",
name: "family_name",
required: true,
}
]}
>
{children}
</CognitoAuth>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ export interface CloudscapeReactTsWebsiteProjectOptions
*/
readonly publicDir?: string;

/**
* Whether to enable self sign-up.
*
* @default false
*/
readonly allowSignup?: boolean;

/**
* TypeSafeApi instance to use when setting up the initial project sample code.
* @deprecated use typeSafeApis
Expand All @@ -54,6 +61,7 @@ export interface CloudscapeReactTsWebsiteProjectOptions
*/
export class CloudscapeReactTsWebsiteProject extends ReactTypeScriptProject {
public readonly applicationName: string;
public readonly allowSignup: boolean;
public readonly publicDir: string;
public readonly typeSafeApis?: TypeSafeApiProject[];
public readonly typeSafeWebSocketApis?: TypeSafeWebSocketApiProject[];
Expand Down Expand Up @@ -106,6 +114,7 @@ export class CloudscapeReactTsWebsiteProject extends ReactTypeScriptProject {
this.testTask.exec("react-scripts test --watchAll=false --passWithNoTests");

this.applicationName = options.applicationName ?? "Sample App";
this.allowSignup = options.allowSignup ?? false;
this.publicDir = options.publicDir ?? "public";
const srcDir = path.resolve(
__dirname,
Expand Down Expand Up @@ -173,6 +182,7 @@ export class CloudscapeReactTsWebsiteProject extends ReactTypeScriptProject {
typeSafeApisReversed: [...apis].reverse(),
typeSafeWebSocketApis: webSocketApis,
typeSafeWebSocketApisReversed: [...webSocketApis].reverse(),
allowSignup: options.allowSignup ?? false,
};

new SampleDir(this, this.srcdir, {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion packages/identity/src/user-identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ const WEB_CLIENT_ID = "WebClient";
* Properties which configures the Identity Pool.
*/
export interface UserIdentityProps {
/**
* Allow self sign up
*
* @default - false
*/
readonly allowSignup?: boolean;

/**
* User provided Cognito UserPool.
*
Expand All @@ -42,7 +49,11 @@ export class UserIdentity extends Construct {

// Unless explicitly stated, created a default Cognito User Pool and Web Client.
this.userPool = !props?.userPool
? new UserPoolWithMfa(this, "UserPool")
? new UserPoolWithMfa(
this,
"UserPool",
props?.allowSignup ? { selfSignUpEnabled: true } : undefined
)
: props.userPool;

this.identityPool = new IdentityPool(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {{{groupId}}}.constructs.apis.{{{apiName}}};
import {{{groupId}}}.constructs.websites.{{{websiteName}}};
{{/cloudscapeReactTsWebsites}}
import software.aws.pdk.identity.UserIdentity;
import software.constructs.Construct;
{{#allowSignup}}import software.aws.pdk.identity.UserIdentityProps;
{{/allowSignup}}import software.constructs.Construct;

public class ApplicationStack extends Stack {
public ApplicationStack(Construct scope, String id) {
Expand All @@ -19,7 +20,10 @@ public class ApplicationStack extends Stack {
public ApplicationStack(Construct scope, String id, StackProps props) {
super(scope, id, props);
UserIdentity userIdentity = new UserIdentity(this, String.format("%sUserIdentity", id));
UserIdentity userIdentity = new UserIdentity(this, String.format("%sUserIdentity", id){{#allowSignup}},
UserIdentityProps.builder()
.allowSignup(true)
.build(){{/allowSignup}});
{{#typeSafeApis}}
{{#cloudscapeReactTsWebsites.0}}{{{apiName}}} {{{apiNameLowercase}}} = {{/cloudscapeReactTsWebsites.0}}new {{{apiName}}}(this, "{{{apiName}}}", userIdentity);
{{/typeSafeApis}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ApplicationStack(Stack):
def __init__(self, scope: Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)

user_identity = UserIdentity(self, '{}UserIdentity'.format(id))
user_identity = UserIdentity(self, '{}UserIdentity'.format(id){{#allowSignup}}, allow_signup=True{{/allowSignup}})
{{#typeSafeApis}}
{{#cloudscapeReactTsWebsites.0}}{{{apiNameLowercase}}} = {{/cloudscapeReactTsWebsites.0}}{{{apiName}}}(self, '{{{apiName}}}', user_identity)
{{/typeSafeApis}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export class ApplicationStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const userIdentity = new UserIdentity(this, `${id}UserIdentity`);
const userIdentity = new UserIdentity(this, `${id}UserIdentity`{{#allowSignup}}, {
allowSignup: true,
}{{/allowSignup}});
{{#typeSafeApis}}
{{#cloudscapeReactTsWebsites.0}}const {{{apiNameLowercase}}} = {{/cloudscapeReactTsWebsites.0}}new {{{apiName}}}(this, "{{{apiName}}}", {
userIdentity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ export interface InfrastructureJavaProjectOptions extends AwsCdkJavaAppOptions {
*/
readonly stackName?: string;

/**
* Allow self sign up for the UserIdentity construct.
*
* @default - false
*/
readonly allowSignup?: boolean;

/**
* TypeSafeApi instance to use when setting up the initial project sample code.
* @deprecated use typeSafeApis
Expand Down Expand Up @@ -149,6 +156,7 @@ export class InfrastructureJavaProject extends AwsCdkJavaApp {

const mustacheConfig = {
stackName: options.stackName || DEFAULT_STACK_NAME,
allowSignup: options.allowSignup ?? false,
groupId,
typeSafeApis: this.generateTypeSafeMustacheConfig(groupId, typeSafeApis),
cloudscapeReactTsWebsites: cloudscapeReactTsWebsites.map((csWebsite) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ export interface InfrastructurePyProjectOptions extends AwsCdkPythonAppOptions {
*/
readonly stackName?: string;

/**
* Allow self sign up for the UserIdentity construct.
*
* @default - false
*/
readonly allowSignup?: boolean;

/**
* TypeSafeApi instance to use when setting up the initial project sample code.
* @deprecated use typeSafeApis
Expand Down Expand Up @@ -128,6 +135,7 @@ export class InfrastructurePyProject extends AwsCdkPythonApp {

const mustacheConfig = {
stackName: options.stackName || DEFAULT_STACK_NAME,
allowSignup: options.allowSignup ?? false,
moduleName,
typeSafeApis: this.generateTypeSafeMustacheConfig(
moduleName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ export interface InfrastructureTsProjectOptions
*/
readonly stackName?: string;

/**
* Allow self sign up for the UserIdentity construct.
*
* @default - false
*/
readonly allowSignup?: boolean;

/**
* TypeSafeApi instance to use when setting up the initial project sample code.
* @deprecated use typeSafeApis
Expand Down Expand Up @@ -135,6 +142,7 @@ export class InfrastructureTsProject extends AwsCdkTypeScriptApp {

const mustacheConfig = {
stackName: options.stackName || DEFAULT_STACK_NAME,
allowSignup: options.allowSignup ?? false,
typeSafeApis: this.generateTypeSafeMustacheConfig(typeSafeApis),
typeSafeWebSocketApis: this.generateTypeSafeMustacheConfig(
typeSafeWebSocketApis
Expand Down

0 comments on commit 300a8db

Please sign in to comment.