@gravitywelluk/aws
Version:
Library of commonly used AWS wrapper functions to communicate with the AWS SDK
94 lines (93 loc) • 4.47 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createCognitoBasicUser = void 0;
const Joi = __importStar(require("joi"));
const validation_utils_1 = require("@gravitywelluk/validation-utils");
const cognito_configure_1 = require("./cognito-configure");
const aws_error_1 = require("../utils/aws-error");
const utils_1 = require("./utils");
/**
* Creates a basic (non-admin) user in Cognito and triggers verification code
*
* This kind of user will be sent a verification code to their email address or phone number
* Entering this and then calling confirmSignUp will enable the user's account
*
* @see https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityServiceProvider.html#signUp-property
* @param createBasicUserParams - The parameters required to create a basic Cognito user
* @param awsCognitoConfigOverrides - Configuration option overrides
*/
const createCognitoBasicUser = async (createBasicUserParams, awsCognitoConfigOverrides = {}) => {
const cognito = (0, cognito_configure_1.cognitoConfigure)(awsCognitoConfigOverrides);
const { error } = Joi.object({
userPoolId: Joi.string().required(),
clientId: Joi.string().required(),
email: Joi.string().email().required(),
password: Joi.string().required(),
groups: Joi.array().items(Joi.string().optional()).optional()
}).validate(createBasicUserParams);
// Error if there any Joi validation errors
if (error) {
throw new validation_utils_1.JoiError(error);
}
// If createUserParams.groups are provided, get the current Cognito groups
// with the given user pool
if (createBasicUserParams.groups && createBasicUserParams.groups.length > 0) {
// Get all of the Cognito groups for the given user pool
const allCognitoGroups = await (0, utils_1.cognitoListGroups)(cognito, { UserPoolId: createBasicUserParams.userPoolId });
// Validate that the given createUserParams.groups match the allCognitoGroups
const { error: joiCognitoGroupsError } = Joi.array().items(Joi.string().valid(...allCognitoGroups).required()).validate(createBasicUserParams.groups);
// Error if there any Joi validation errors regarding the given groups now
// we have sight of the groups that can be chosen (allCognitoGroups)
if (joiCognitoGroupsError) {
throw new validation_utils_1.JoiError(joiCognitoGroupsError);
}
}
// Create the Cognito user and add them to the given groups
try {
const response = await cognito.signUp({
ClientId: createBasicUserParams.clientId,
Username: createBasicUserParams.email,
Password: createBasicUserParams.password,
UserAttributes: [
{
Name: "email",
Value: createBasicUserParams.email
}
]
}).promise();
// If createUserParams.groups are provided, add the user to the given groups
if (createBasicUserParams.groups && createBasicUserParams.groups.length > 0) {
for (const group of createBasicUserParams.groups) {
await cognito.adminAddUserToGroup({
GroupName: group,
Username: createBasicUserParams.email,
UserPoolId: createBasicUserParams.userPoolId
}).promise();
}
}
return response;
}
catch (error) {
throw new aws_error_1.AwsError(error);
}
};
exports.createCognitoBasicUser = createCognitoBasicUser;