Comparing version 1.1.3 to 1.1.4
@@ -31,7 +31,14 @@ /** | ||
* Create a new user (by its email) in the pool specified. | ||
* @param {string} email the email to login of the new user | ||
* @param {string} email the email to use as login | ||
* @param {string} cognitoUserPoolId the pool in which to create the user | ||
* @param {any} options | ||
``` | ||
{ | ||
noNotification??: boolean; // if true, don't send the default Cognito email notification | ||
temporaryPassword?: string; // if null, randomly generated | ||
} | ||
``` | ||
* @return {Promise<string>} userId of the new user | ||
*/ | ||
createUser(email: string, cognitoUserPoolId: string): Promise<string>; | ||
createUser(email: string, cognitoUserPoolId: string, options?: any): Promise<string>; | ||
} |
@@ -79,17 +79,37 @@ "use strict"; | ||
* Create a new user (by its email) in the pool specified. | ||
* @param {string} email the email to login of the new user | ||
* @param {string} email the email to use as login | ||
* @param {string} cognitoUserPoolId the pool in which to create the user | ||
* @param {any} options | ||
``` | ||
{ | ||
noNotification??: boolean; // if true, don't send the default Cognito email notification | ||
temporaryPassword?: string; // if null, randomly generated | ||
} | ||
``` | ||
* @return {Promise<string>} userId of the new user | ||
*/ | ||
createUser(email, cognitoUserPoolId) { | ||
createUser(email, cognitoUserPoolId, options) { | ||
return new Promise((resolve, reject) => { | ||
options = options || {}; | ||
if (IdeaX.isEmpty(email, 'email')) | ||
return reject(new Error(`E.COGNITO.INVALID_EMAIL`)); | ||
let attributes = [{ Name: 'email', Value: email }, { Name: 'email_verified', Value: 'true' }]; | ||
new AWS.CognitoIdentityServiceProvider().adminCreateUser({ | ||
let params = { | ||
UserPoolId: cognitoUserPoolId, Username: email, UserAttributes: attributes | ||
}, (err, data) => { | ||
}; | ||
if (options.noNotification) | ||
params.MessageAction = 'SUPPRESS'; | ||
if (options.temporaryPassword) | ||
params.TemporaryPassword = options.temporaryPassword; | ||
new AWS.CognitoIdentityServiceProvider().adminCreateUser(params, (err, data) => { | ||
IdeaX.logger('COGNITO CREATE USER', err, data); | ||
if (err) | ||
return reject(err); | ||
if (err) { | ||
switch (err.name) { | ||
case 'UsernameExistsException': | ||
return reject(new Error(`E.COGNITO.USERNAME_ALREADY_EXISTS`)); | ||
case 'InvalidPasswordException': | ||
return reject(new Error(`E.COGNITO.INVALID_PASSWORD`)); | ||
default: return reject(err); | ||
} | ||
} | ||
let userId = data.User.Attributes.find((attr) => attr.Name == 'sub').Value || null; | ||
@@ -99,3 +119,3 @@ if (userId) | ||
else | ||
reject(new Error(`E.COGNITO.INVALID_USER_ID`)); | ||
reject(new Error(`E.COGNITO.CREATION_FAILED`)); | ||
}); | ||
@@ -102,0 +122,0 @@ }); |
{ | ||
"name": "idea-aws", | ||
"version": "1.1.3", | ||
"version": "1.1.4", | ||
"description": "AWS wrappers to use in IDEA's back-ends", | ||
@@ -5,0 +5,0 @@ "engines": { |
@@ -79,18 +79,37 @@ import AWS = require('aws-sdk'); | ||
* Create a new user (by its email) in the pool specified. | ||
* @param {string} email the email to login of the new user | ||
* @param {string} email the email to use as login | ||
* @param {string} cognitoUserPoolId the pool in which to create the user | ||
* @param {any} options | ||
``` | ||
{ | ||
noNotification??: boolean; // if true, don't send the default Cognito email notification | ||
temporaryPassword?: string; // if null, randomly generated | ||
} | ||
``` | ||
* @return {Promise<string>} userId of the new user | ||
*/ | ||
public createUser(email: string, cognitoUserPoolId: string): Promise<string> { | ||
public createUser(email: string, cognitoUserPoolId: string, options? : any): Promise<string> { | ||
return new Promise((resolve, reject) => { | ||
options = options || {}; | ||
if(IdeaX.isEmpty(email, 'email')) return reject(new Error(`E.COGNITO.INVALID_EMAIL`)); | ||
let attributes = [{ Name: 'email', Value: email }, { Name: 'email_verified', Value: 'true' }]; | ||
new AWS.CognitoIdentityServiceProvider().adminCreateUser({ | ||
let params = <any> { | ||
UserPoolId: cognitoUserPoolId, Username: email, UserAttributes: attributes | ||
}, (err: Error, data: any) => { | ||
}; | ||
if(options.noNotification) params.MessageAction = 'SUPPRESS'; | ||
if(options.temporaryPassword) params.TemporaryPassword = options.temporaryPassword; | ||
new AWS.CognitoIdentityServiceProvider().adminCreateUser(params, (err: Error, data: any) => { | ||
IdeaX.logger('COGNITO CREATE USER', err, data); | ||
if(err) return reject(err); | ||
if(err) { | ||
switch(err.name) { | ||
case 'UsernameExistsException': | ||
return reject(new Error(`E.COGNITO.USERNAME_ALREADY_EXISTS`)); | ||
case 'InvalidPasswordException': | ||
return reject(new Error(`E.COGNITO.INVALID_PASSWORD`)); | ||
default: return reject(err); | ||
} | ||
} | ||
let userId = data.User.Attributes.find((attr: any) => attr.Name == 'sub').Value || null; | ||
if(userId) resolve(userId); | ||
else reject(new Error(`E.COGNITO.INVALID_USER_ID`)); | ||
else reject(new Error(`E.COGNITO.CREATION_FAILED`)); | ||
}); | ||
@@ -97,0 +116,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
2960637
3373
45