AWS Lambda Function to Register in Cognito
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
This is the entire code for a Lambda function that registers a new user in Amazon Cognito.
The email, username, and password are passed to the Lambda function. The client ID is stored in an environment variable to be removed from the code.
const AWS = require("aws-sdk");
const cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();
const clientID1 = process.env.client_id;
function registerUser(client, password, username, email)
var params =
ClientId: client, /* required */
Password: password, /* required */
Username: username, /* required */
UserAttributes: [
Name: 'email',
Value: email
]
;
cognitoidentityserviceprovider.signUp(params, function(err, data)
if (err) console.log(err, err.stack); // an error occurred
else
console.log(data); // successful response
return data;
);
function verifyInput(e)
let data = ;
try
data.password = e.password;
data.username = e.username;
data.email = e.email;
catch (e)
console.error(e);
return false;
if (data.password === undefined) return false;
if (data.username === undefined) return false;
if (data.email === undefined) return false;
return data;
exports.handler = (event, context, callback) =>
let check = verifyInput(event);
if (check === false) return callback(null, "Registration Failed");
let data = registerUser(clientID1, event.password, event.username, event.email);
callback(null, "Registration Successful");
;
Note: I am aware that returning the data object from the verifyInput function is currently useless. I left it in for now as I thought there may need to still be some checks on the parameters at this stage.
Why I am using Lambda for the login?
I have created a login system a couple of times (not sure how well) but would like to start using Cognito. For implementing on websites the JavaScript SDK seems to be best supported/documented. For simple/small/static sites I'd prefer not to implement a JavaScript backend but AWS PHP SDK doesn't seem to be well supported or documented and without working examples for a Cognito login. So I figured I could use NodeJS Lambda Functions to use the well supported JavaScript SDK then have PHP access them through an Amazon API Gateway.
javascript node.js lambda amazon-web-services
add a comment |Â
up vote
2
down vote
favorite
This is the entire code for a Lambda function that registers a new user in Amazon Cognito.
The email, username, and password are passed to the Lambda function. The client ID is stored in an environment variable to be removed from the code.
const AWS = require("aws-sdk");
const cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();
const clientID1 = process.env.client_id;
function registerUser(client, password, username, email)
var params =
ClientId: client, /* required */
Password: password, /* required */
Username: username, /* required */
UserAttributes: [
Name: 'email',
Value: email
]
;
cognitoidentityserviceprovider.signUp(params, function(err, data)
if (err) console.log(err, err.stack); // an error occurred
else
console.log(data); // successful response
return data;
);
function verifyInput(e)
let data = ;
try
data.password = e.password;
data.username = e.username;
data.email = e.email;
catch (e)
console.error(e);
return false;
if (data.password === undefined) return false;
if (data.username === undefined) return false;
if (data.email === undefined) return false;
return data;
exports.handler = (event, context, callback) =>
let check = verifyInput(event);
if (check === false) return callback(null, "Registration Failed");
let data = registerUser(clientID1, event.password, event.username, event.email);
callback(null, "Registration Successful");
;
Note: I am aware that returning the data object from the verifyInput function is currently useless. I left it in for now as I thought there may need to still be some checks on the parameters at this stage.
Why I am using Lambda for the login?
I have created a login system a couple of times (not sure how well) but would like to start using Cognito. For implementing on websites the JavaScript SDK seems to be best supported/documented. For simple/small/static sites I'd prefer not to implement a JavaScript backend but AWS PHP SDK doesn't seem to be well supported or documented and without working examples for a Cognito login. So I figured I could use NodeJS Lambda Functions to use the well supported JavaScript SDK then have PHP access them through an Amazon API Gateway.
javascript node.js lambda amazon-web-services
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
This is the entire code for a Lambda function that registers a new user in Amazon Cognito.
The email, username, and password are passed to the Lambda function. The client ID is stored in an environment variable to be removed from the code.
const AWS = require("aws-sdk");
const cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();
const clientID1 = process.env.client_id;
function registerUser(client, password, username, email)
var params =
ClientId: client, /* required */
Password: password, /* required */
Username: username, /* required */
UserAttributes: [
Name: 'email',
Value: email
]
;
cognitoidentityserviceprovider.signUp(params, function(err, data)
if (err) console.log(err, err.stack); // an error occurred
else
console.log(data); // successful response
return data;
);
function verifyInput(e)
let data = ;
try
data.password = e.password;
data.username = e.username;
data.email = e.email;
catch (e)
console.error(e);
return false;
if (data.password === undefined) return false;
if (data.username === undefined) return false;
if (data.email === undefined) return false;
return data;
exports.handler = (event, context, callback) =>
let check = verifyInput(event);
if (check === false) return callback(null, "Registration Failed");
let data = registerUser(clientID1, event.password, event.username, event.email);
callback(null, "Registration Successful");
;
Note: I am aware that returning the data object from the verifyInput function is currently useless. I left it in for now as I thought there may need to still be some checks on the parameters at this stage.
Why I am using Lambda for the login?
I have created a login system a couple of times (not sure how well) but would like to start using Cognito. For implementing on websites the JavaScript SDK seems to be best supported/documented. For simple/small/static sites I'd prefer not to implement a JavaScript backend but AWS PHP SDK doesn't seem to be well supported or documented and without working examples for a Cognito login. So I figured I could use NodeJS Lambda Functions to use the well supported JavaScript SDK then have PHP access them through an Amazon API Gateway.
javascript node.js lambda amazon-web-services
This is the entire code for a Lambda function that registers a new user in Amazon Cognito.
The email, username, and password are passed to the Lambda function. The client ID is stored in an environment variable to be removed from the code.
const AWS = require("aws-sdk");
const cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();
const clientID1 = process.env.client_id;
function registerUser(client, password, username, email)
var params =
ClientId: client, /* required */
Password: password, /* required */
Username: username, /* required */
UserAttributes: [
Name: 'email',
Value: email
]
;
cognitoidentityserviceprovider.signUp(params, function(err, data)
if (err) console.log(err, err.stack); // an error occurred
else
console.log(data); // successful response
return data;
);
function verifyInput(e)
let data = ;
try
data.password = e.password;
data.username = e.username;
data.email = e.email;
catch (e)
console.error(e);
return false;
if (data.password === undefined) return false;
if (data.username === undefined) return false;
if (data.email === undefined) return false;
return data;
exports.handler = (event, context, callback) =>
let check = verifyInput(event);
if (check === false) return callback(null, "Registration Failed");
let data = registerUser(clientID1, event.password, event.username, event.email);
callback(null, "Registration Successful");
;
Note: I am aware that returning the data object from the verifyInput function is currently useless. I left it in for now as I thought there may need to still be some checks on the parameters at this stage.
Why I am using Lambda for the login?
I have created a login system a couple of times (not sure how well) but would like to start using Cognito. For implementing on websites the JavaScript SDK seems to be best supported/documented. For simple/small/static sites I'd prefer not to implement a JavaScript backend but AWS PHP SDK doesn't seem to be well supported or documented and without working examples for a Cognito login. So I figured I could use NodeJS Lambda Functions to use the well supported JavaScript SDK then have PHP access them through an Amazon API Gateway.
javascript node.js lambda amazon-web-services
edited May 28 at 1:28
Jamalâ¦
30.1k11114225
30.1k11114225
asked May 21 at 21:13
Lou Bagel
615
615
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194900%2faws-lambda-function-to-register-in-cognito%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password