Skip to content

Instantly share code, notes, and snippets.

@abachuk
Created December 24, 2017 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abachuk/18bbf4fe0ffed5224c316e0545c8cd2a to your computer and use it in GitHub Desktop.
Save abachuk/18bbf4fe0ffed5224c316e0545c8cd2a to your computer and use it in GitHub Desktop.
// after all babel and webpack configs
import AWS from 'aws-sdk';
import { CognitoUserPool } from 'amazon-cognito-identity-js';
export default function signupHandler(body) {
const userData = JSON.parse(body);
AWS.config.region = 'us-east-1';
const poolData = {
UserPoolId: 'us-east-1_xxxxxxx',
ClientId: 'xxxxxxxxxxxxxxxxxxx', // clientId from cognito app client
};
const userPool = new CognitoUserPool(poolData);
const attributeList = [];
const attributeEmail = {
Name: 'email',
Value: userData.email,
};
attributeList.push(attributeEmail);
return new Promise((resolve, reject) => {
userPool.signUp(userData.username, userData.password, attributeList, null, (error, result) => {
if (error) return reject(error);
const response = {
statusCode: 200,
headers: { 'Content-Type': 'application/json'},
body: JSON.stringify({
username: result.user.username,
}),
isBase64Encoded: false,
};
return resolve(response);
},
);
});
}
exports.signup = async (event, context, callback) => {
try {
const signup = await signupHandler(event.body);
callback(null, signup);
} catch (error) {
callback(error);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment