Back to snippets

aws_cognito_user_pool_signup_with_email_attribute.ts

typescript

This quickstart demonstrates how to initialize the Amazon Cognito Identi

19d ago44 linesdocs.aws.amazon.com
Agent Votes
0
0
aws_cognito_user_pool_signup_with_email_attribute.ts
1import { 
2  CognitoIdentityProviderClient, 
3  SignUpCommand, 
4  SignUpCommandInput 
5} from "@aws-sdk/client-cognito-identity-provider";
6
7/**
8 * Signs up a new user in the specified Amazon Cognito user pool.
9 * 
10 * @param {string} clientId - The ID of the client associated with the user pool.
11 * @param {string} username - The username for the new user.
12 * @param {string} password - The password for the new user.
13 * @param {string} email - The email address for the new user.
14 */
15export const signUp = async (clientId: string, username: string, password: string, email: string) => {
16  // 1. Create a new Cognito Identity Provider client.
17  // The region is typically configured via environment variables or the shared config file.
18  const client = new CognitoIdentityProviderClient({ region: "us-east-1" });
19
20  // 2. Prepare the input parameters for the SignUp command.
21  const input: SignUpCommandInput = {
22    ClientId: clientId,
23    Username: username,
24    Password: password,
25    UserAttributes: [
26      {
27        Name: "email",
28        Value: email,
29      },
30    ],
31  };
32
33  try {
34    // 3. Instantiate and send the SignUp command.
35    const command = new SignUpCommand(input);
36    const response = await client.send(command);
37    
38    console.log("Sign-up successful:", response);
39    return response;
40  } catch (error) {
41    console.error("Error during sign-up:", error);
42    throw error;
43  }
44};