I'm trying to create a new user in cognito but the signup method not working for some reason, I also finished configuring the CLI and it working correctly.
here is my code
const Auth = require('aws-amplify').Auth;
global.fetch = require('node-fetch');
Auth.configure({
Auth: {
// REQUIRED only for Federated Authentication - Amazon Cognito Identity Pool ID
identityPoolId: "I already put the identity Pool Id here",
// REQUIRED - Amazon Cognito Region
region: "us-east-2",
// OPTIONAL - Amazon Cognito Federated Identity Pool Region
// Required only if it's different from Amazon Cognito Region
identityPoolRegion: "us-east-2",
// OPTIONAL - Amazon Cognito User Pool ID
userPoolId: "I already put the user Pool Id here",
// OPTIONAL - Amazon Cognito Web Client ID (26-char alphanumeric string)
userPoolWebClientId: "I already put the user Pool Web Client Id here",
// OPTIONAL - Enforce user authentication prior to accessing AWS resources or not
mandatorySignIn: false,
// OPTIONAL - Hosted UI configuration
oauth: {}
}
});
exports.handler = async (event) => {
const email = event.email;
const password = event.pass;
const fullName = event.fName;
const phone = event.phone;
Auth.signUp({
username: email,
password: password,
attributes: {
'email': email,
'phone_number': phone,
'name': fullName
}
}).then( function (data) {
console.log("1");
const response = {
statusCode: 200,
body: JSON.stringify(data.user)
};
return response;
}).catch(function (err) {
console.log("2");
const response = {
statusCode: 520,
body: JSON.stringify(err)
};
return response;
});
console.log("3");
const response = {
statusCode: 200,
body: JSON.stringify("Nothing")
};
return response;
}
And this is the output:
INFO 3
And this is the response:
{ "statusCode": 200, "body": "\"Nothing\"" }
by the way, I tried amazon-cognito-identity-js but it has the same problem.
You seem to be triggering the signup properly, but then you do not wait for the response, instead you immediately return a custom response ("Nothing") that has nothing to do with the actual response from signUp.
Try returning the promise that you get from calling the Auth.signup
function instead, like this.
exports.handler = async (event) => {
const email = event.email;
const password = event.pass;
const fullName = event.fName;
const phone = event.phone;
// NOTICE! Here is where you return the chained promise resulting
// from the then(...) and catch(...)
return Auth.signUp({
username: email,
password: password,
attributes: {
'email': email,
'phone_number': phone,
'name': fullName
}
}).then( function (data) {
return {
statusCode: 200,
body: JSON.stringify(data.user)
};
}).catch(function (err) {
return {
statusCode: 520,
body: JSON.stringify(err)
};
});
}