Core user pool operations including configuration, user registration, and pool-level management functionality.
The main class for managing a Cognito User Pool and performing pool-level operations.
/**
* Constructs a new CognitoUserPool object
* @param data - Configuration data for the user pool
* @param wrapRefreshSessionCallback - Optional callback wrapper for session refresh
*/
class CognitoUserPool {
constructor(
data: ICognitoUserPoolData,
wrapRefreshSessionCallback?: (target: NodeCallback.Any) => NodeCallback.Any
);
/** Returns the user pool ID */
getUserPoolId(): string;
/** Returns the user pool name derived from the pool ID */
getUserPoolName(): string;
/** Returns the client ID */
getClientId(): string;
/**
* Registers a new user in the user pool
* @param username - The username for the new user
* @param password - The temporary password for the new user
* @param userAttributes - Array of user attributes
* @param validationData - Array of validation data attributes
* @param callback - Callback function to handle the result
* @param clientMetadata - Optional client metadata for Lambda triggers
*/
signUp(
username: string,
password: string,
userAttributes: CognitoUserAttribute[],
validationData: CognitoUserAttribute[],
callback: NodeCallback<Error, ISignUpResult>,
clientMetadata?: ClientMetadata
): void;
/**
* Gets the current authenticated user from storage
* @returns CognitoUser instance if found, null otherwise
*/
getCurrentUser(): CognitoUser | null;
}
interface ICognitoUserPoolData {
/** Cognito user pool ID (format: region_poolId) */
UserPoolId: string;
/** User pool application client ID */
ClientId: string;
/** Optional custom service endpoint */
endpoint?: string;
/** Optional storage implementation for session persistence */
Storage?: ICognitoStorage;
/**
* Optional flag for advanced security data collection
* Enables/disables data collection for Cognito advanced security features
* Defaults to true
*/
AdvancedSecurityDataCollectionFlag?: boolean;
}
interface ISignUpResult {
/** The newly created CognitoUser instance */
user: CognitoUser;
/** Whether the user account is confirmed */
userConfirmed: boolean;
/** The user's unique identifier (sub) */
userSub: string;
/** Details about how the confirmation code was delivered */
codeDeliveryDetails: CodeDeliveryDetails;
}Usage Examples:
import { CognitoUserPool, CognitoUserAttribute } from "amazon-cognito-identity-js";
// Basic user pool setup
const userPool = new CognitoUserPool({
UserPoolId: "us-east-1_XXXXXXXXX",
ClientId: "your-client-id"
});
// User pool with custom storage
const userPoolWithStorage = new CognitoUserPool({
UserPoolId: "us-east-1_XXXXXXXXX",
ClientId: "your-client-id",
Storage: customStorageImplementation
});
// Register a new user
const userAttributes = [
new CognitoUserAttribute({ Name: "email", Value: "user@example.com" }),
new CognitoUserAttribute({ Name: "given_name", Value: "John" }),
new CognitoUserAttribute({ Name: "family_name", Value: "Doe" })
];
userPool.signUp(
"johndoe",
"TempPassword123!",
userAttributes,
[], // validation data
(err, result) => {
if (err) {
console.error("Sign up failed:", err);
return;
}
console.log("User signed up successfully:", result?.user.getUsername());
console.log("Confirmation required:", !result?.userConfirmed);
}
);
// Get current user
const currentUser = userPool.getCurrentUser();
if (currentUser) {
console.log("Current user:", currentUser.getUsername());
}Understanding user pool configuration and setup options.
Key Configuration Parameters:
region_poolId)Storage Implementation:
The SDK supports pluggable storage through the ICognitoStorage interface:
interface ICognitoStorage {
setItem(key: string, value: string): void;
getItem(key: string): string | null;
removeItem(key: string): void;
clear(): void;
}Built-in storage options include browser localStorage (default) and CookieStorage class for cookie-based persistence.
Common errors when working with user pools:
region_poolId// Error handling example
userPool.signUp(username, password, attributes, [], (err, result) => {
if (err) {
switch (err.code) {
case 'UsernameExistsException':
console.error("Username already exists");
break;
case 'InvalidParameterException':
console.error("Invalid parameter:", err.message);
break;
case 'InvalidPasswordException':
console.error("Password does not meet requirements");
break;
default:
console.error("Sign up error:", err);
}
return;
}
// Handle success
});