Skip to content

Commit

Permalink
feat(cognito): add pre sign-up trigger (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoegertn committed Oct 10, 2023
1 parent 9d85e5d commit f57437e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/constructs/assets/search.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions docs/constructs/classes/CognitoAuthentication.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ <h3 class="tsd-index-heading">Properties</h3>
<div class="tsd-index-list"><a href="CognitoAuthentication.html#customMessageFunction" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><g id="icon-1024"><rect fill="var(--color-icon-background)" stroke="#FF984D" stroke-width="1.5" x="1" y="1" width="22" height="22" rx="12"></rect><path d="M9.354 16V7.24H12.174C12.99 7.24 13.638 7.476 14.118 7.948C14.606 8.412 14.85 9.036 14.85 9.82C14.85 10.604 14.606 11.232 14.118 11.704C13.638 12.168 12.99 12.4 12.174 12.4H10.434V16H9.354ZM10.434 11.428H12.174C12.646 11.428 13.022 11.284 13.302 10.996C13.59 10.7 13.734 10.308 13.734 9.82C13.734 9.324 13.59 8.932 13.302 8.644C13.022 8.356 12.646 8.212 12.174 8.212H10.434V11.428Z" fill="var(--color-text)"></path></g></svg><span>custom<wbr/>Message<wbr/>Function?</span></a>
<a href="CognitoAuthentication.html#identityPool" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-1024"></use></svg><span>identity<wbr/>Pool?</span></a>
<a href="CognitoAuthentication.html#node" class="tsd-index-link tsd-is-inherited tsd-is-external"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-1024"></use></svg><span>node</span></a>
<a href="CognitoAuthentication.html#preSignUpFunction" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-1024"></use></svg><span>pre<wbr/>Sign<wbr/>Up<wbr/>Function?</span></a>
<a href="CognitoAuthentication.html#preTokenGenerationFunction" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-1024"></use></svg><span>pre<wbr/>Token<wbr/>Generation<wbr/>Function?</span></a>
<a href="CognitoAuthentication.html#userpool" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-1024"></use></svg><span>userpool</span></a>
</div></section>
Expand Down Expand Up @@ -82,6 +83,9 @@ <h3 class="tsd-anchor-link"><code class="tsd-tag ts-flagReadonly">Readonly</code
<h4>Stability</h4><p>stable</p>
</div><aside class="tsd-sources">
<p>Inherited from Construct.node</p></aside></section>
<section class="tsd-panel tsd-member"><a id="preSignUpFunction" class="tsd-anchor"></a>
<h3 class="tsd-anchor-link"><code class="tsd-tag ts-flagOptional">Optional</code> <code class="tsd-tag ts-flagReadonly">Readonly</code> <span>pre<wbr/>Sign<wbr/>Up<wbr/>Function</span><a href="#preSignUpFunction" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="#icon-anchor"></use></svg></a></h3>
<div class="tsd-signature"><span class="tsd-kind-property">pre<wbr/>Sign<wbr/>Up<wbr/>Function</span><span class="tsd-signature-symbol">?:</span> <a href="LambdaFunction.html" class="tsd-signature-type tsd-kind-class">LambdaFunction</a></div></section>
<section class="tsd-panel tsd-member"><a id="preTokenGenerationFunction" class="tsd-anchor"></a>
<h3 class="tsd-anchor-link"><code class="tsd-tag ts-flagOptional">Optional</code> <code class="tsd-tag ts-flagReadonly">Readonly</code> <span>pre<wbr/>Token<wbr/>Generation<wbr/>Function</span><a href="#preTokenGenerationFunction" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="#icon-anchor"></use></svg></a></h3>
<div class="tsd-signature"><span class="tsd-kind-property">pre<wbr/>Token<wbr/>Generation<wbr/>Function</span><span class="tsd-signature-symbol">?:</span> <a href="LambdaFunction.html" class="tsd-signature-type tsd-kind-class">LambdaFunction</a></div></section>
Expand Down Expand Up @@ -149,6 +153,7 @@ <h3><svg width="20" height="20" viewBox="0 0 24 24" fill="none"><use href="#icon
<li><a href="#customMessageFunction" class=""><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-1024"></use></svg><span>custom<wbr/>Message<wbr/>Function</span></a></li>
<li><a href="#identityPool" class=""><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-1024"></use></svg><span>identity<wbr/>Pool</span></a></li>
<li><a href="#node" class="tsd-is-inherited tsd-is-external"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-1024"></use></svg><span>node</span></a></li>
<li><a href="#preSignUpFunction" class=""><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-1024"></use></svg><span>pre<wbr/>Sign<wbr/>Up<wbr/>Function</span></a></li>
<li><a href="#preTokenGenerationFunction" class=""><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-1024"></use></svg><span>pre<wbr/>Token<wbr/>Generation<wbr/>Function</span></a></li>
<li><a href="#userpool" class=""><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-1024"></use></svg><span>userpool</span></a></li>
<li><a href="#toString" class="tsd-is-inherited tsd-is-external"><svg class="tsd-kind-icon" viewBox="0 0 24 24"><use href="#icon-2048"></use></svg><span>to<wbr/>String</span></a></li>
Expand Down
11 changes: 10 additions & 1 deletion docs/constructs/interfaces/CognitoAuthenticationProps.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ <h5><code class="tsd-tag ts-flagReadonly">Readonly</code> <span class="tsd-kind-
<div class="tsd-comment tsd-typography"></div></section>
<section class="tsd-panel tsd-member"><a id="triggers" class="tsd-anchor"></a>
<h3 class="tsd-anchor-link"><code class="tsd-tag ts-flagOptional">Optional</code> <code class="tsd-tag ts-flagReadonly">Readonly</code> <span>triggers</span><a href="#triggers" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24"><use href="#icon-anchor"></use></svg></a></h3>
<div class="tsd-signature"><span class="tsd-kind-property">triggers</span><span class="tsd-signature-symbol">?:</span> <span class="tsd-signature-symbol">{ </span><br/><span>    </span><span class="tsd-kind-property">customMessages</span><span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">boolean</span><span class="tsd-signature-symbol">; </span><br/><span>    </span><span class="tsd-kind-property">preTokenGeneration</span><span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">boolean</span><span class="tsd-signature-symbol">; </span><br/><span class="tsd-signature-symbol">}</span></div>
<div class="tsd-signature"><span class="tsd-kind-property">triggers</span><span class="tsd-signature-symbol">?:</span> <span class="tsd-signature-symbol">{ </span><br/><span>    </span><span class="tsd-kind-property">customMessages</span><span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">boolean</span><span class="tsd-signature-symbol">; </span><br/><span>    </span><span class="tsd-kind-property">preSignUp</span><span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">boolean</span><span class="tsd-signature-symbol">; </span><br/><span>    </span><span class="tsd-kind-property">preTokenGeneration</span><span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">boolean</span><span class="tsd-signature-symbol">; </span><br/><span class="tsd-signature-symbol">}</span></div>
<div class="tsd-comment tsd-typography"><p>Configure Cognito Lambda triggers</p>
</div>
<div class="tsd-type-declaration">
Expand All @@ -104,6 +104,15 @@ <h4>Default</h4><pre><code class="language-ts"><span class="hl-3">false</span>
</code><button>Copy</button></pre>
</div></li>
<li class="tsd-parameter">
<h5><code class="tsd-tag ts-flagOptional">Optional</code> <code class="tsd-tag ts-flagReadonly">Readonly</code> <span class="tsd-kind-property">pre<wbr/>Sign<wbr/>Up</span><span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">boolean</span></h5>
<div class="tsd-comment tsd-typography"><p>Attaches a lambda function to the pre sign-up trigger</p>
<p>Code has to reside in &#39;./src/lambda/cognito.pre-signup.ts&#39; with a method &#39;handler&#39;</p>
</div>
<div class="tsd-comment tsd-typography">
<h4>Default</h4><pre><code class="language-ts"><span class="hl-3">false</span>
</code><button>Copy</button></pre>
</div></li>
<li class="tsd-parameter">
<h5><code class="tsd-tag ts-flagOptional">Optional</code> <code class="tsd-tag ts-flagReadonly">Readonly</code> <span class="tsd-kind-property">pre<wbr/>Token<wbr/>Generation</span><span class="tsd-signature-symbol">?: </span><span class="tsd-signature-type">boolean</span></h5>
<div class="tsd-comment tsd-typography"><p>Attaches a lambda function to the pre token generation trigger</p>
<p>Code has to reside in &#39;./src/lambda/cognito.pre-token-generation.ts&#39; with a method &#39;handler&#39;</p>
Expand Down
45 changes: 45 additions & 0 deletions src/constructs/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ export interface CognitoAuthenticationProps {
* @default false
*/
readonly preTokenGeneration?: boolean;
/**
* Attaches a lambda function to the pre sign-up trigger
*
* Code has to reside in './src/lambda/cognito.pre-signup.ts' with a method 'handler'
*
* @default false
*/
readonly preSignUp?: boolean;
};

/**
Expand Down Expand Up @@ -98,7 +106,9 @@ export class CognitoAuthentication extends Construct implements ICognitoAuthenti

public readonly userpool: cognito.UserPool;
public readonly identityPool?: identitypool.IdentityPool;

public readonly customMessageFunction?: LambdaFunction;
public readonly preSignUpFunction?: LambdaFunction;
public readonly preTokenGenerationFunction?: LambdaFunction;

constructor(scope: Construct, id: string, props: CognitoAuthenticationProps) {
Expand Down Expand Up @@ -191,6 +201,41 @@ export class CognitoAuthentication extends Construct implements ICognitoAuthenti
this.userpool.addTrigger(cognito.UserPoolOperation.PRE_TOKEN_GENERATION, this.preTokenGenerationFunction);
}

if (props.triggers?.preSignUp) {
const entryFile = './src/lambda/cognito.pre-signup.ts';

if (!fs.existsSync(entryFile)) {
fs.writeFileSync(entryFile, `export async function handler(event: AWSLambda.PreSignUpTriggerEvent): Promise<AWSLambda.PreSignUpTriggerEvent> {
console.log(JSON.stringify(event));
// Confirm the user
// event.response.autoConfirmUser = true;
// Set the email as verified if it is in the request
// if (event.request.userAttributes.hasOwnProperty('email')) {
// event.response.autoVerifyEmail = true;
// }
// Set the phone number as verified if it is in the request
// if (event.request.userAttributes.hasOwnProperty('phone_number')) {
// event.response.autoVerifyPhone = true;
// }
return event;
}`, {
encoding: 'utf-8',
});
}

this.preSignUpFunction = new LambdaFunction(this, 'PreSignUpFunction', {
lambdaOptions: {
timeout: Duration.seconds(5),
},
entry: entryFile,
});
this.userpool.addTrigger(cognito.UserPoolOperation.PRE_SIGN_UP, this.preSignUpFunction);
}

if (props.sesEmailSender) {
(this.userpool.node.defaultChild as cognito.CfnUserPool).emailConfiguration = {
emailSendingAccount: 'DEVELOPER',
Expand Down

0 comments on commit f57437e

Please sign in to comment.