AWS Lambda Function to Register in Cognito

The name of the pictureThe name of the pictureThe name of the pictureClash 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.







share|improve this question



























    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.







    share|improve this question























      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.







      share|improve this question













      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.









      share|improve this question












      share|improve this question




      share|improve this question








      edited May 28 at 1:28









      Jamal♦

      30.1k11114225




      30.1k11114225









      asked May 21 at 21:13









      Lou Bagel

      615




      615

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          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



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          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













































































          Popular posts from this blog

          Greedy Best First Search implementation in Rust

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          C++11 CLH Lock Implementation