Advertisement
Guest User

handler.js

a guest
Jul 20th, 2017
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. var AWS = require("aws-sdk");
  4. var jwt = require('jsonwebtoken');
  5.  
  6. var dynamodb = new AWS.DynamoDB.DocumentClient({
  7.   region: 'localhost',
  8.   endpoint: 'http://localhost:8000'
  9. });
  10.  
  11. const generatePolicy = function (principalId, effect, resource) {
  12.   const authResponse = {};
  13.   authResponse.principalId = principalId;
  14.   if (effect && resource) {
  15.     const policyDocument = {};
  16.     policyDocument.Version = '2012-10-17';
  17.     policyDocument.Statement = [];
  18.     const statementOne = {};
  19.     statementOne.Action = 'execute-api:Invoke';
  20.     statementOne.Effect = effect;
  21.     statementOne.Resource = resource;
  22.     policyDocument.Statement[0] = statementOne;
  23.     authResponse.policyDocument = policyDocument;
  24.   }
  25.   return authResponse;
  26. };
  27.  
  28. module.exports.hello = (event, context, callback) => {
  29.   const response = {
  30.     statusCode: 200,
  31.     body: JSON.stringify({
  32.       message: 'Your function Hello executed successfully!',
  33.       //input: event,
  34.     }),
  35.   };
  36.   callback(null, response);
  37. };
  38.  
  39. //authorizer
  40. module.exports.auth = (event, context, callback) => {
  41.   console.log('loading function');
  42.   console.log(event);
  43.   console.log(context);
  44.  
  45.   var token = event.authorizationToken;
  46.   console.log(token);
  47.   if (token == 'valid') {
  48.     callback(null, generatePolicy('user', 'Allow', event.methodArn));
  49.   } else {
  50.     callback('Unauthorized');
  51.   }
  52.  
  53. };
  54. // get user
  55. module.exports.getUser = (event, context, callback) => {
  56.   var params = {
  57.     TableName: 'Users',
  58.     Key: {
  59.       email: "abc1@abc.com",
  60.       password: "12346"
  61.     },
  62.   };
  63.   dynamodb.get(params, function (err, data) {
  64.     if (err) {
  65.       console.error(error);
  66.       callback(new Error('Couldn\'t fetch the todo item.'));
  67.       return;
  68.     }
  69.     else {
  70.       const response = {
  71.         statusCode: 200,
  72.         body: JSON.stringify(data),
  73.       };
  74.  
  75.       callback(null, response);
  76.     }
  77.   });
  78. }
  79. //log in user
  80. module.exports.login = (event, context, callback) => {
  81.  
  82.   const dataBody = JSON.parse(event.body);
  83.   var email = dataBody.email;
  84.   var password = dataBody.password;
  85.   var params = {
  86.     TableName: 'Users',
  87.     Key: {
  88.       email: dataBody.email,
  89.       password: dataBody.password
  90.     },
  91.   };
  92.   dynamodb.get(params, function (err, data) {
  93.     if (err) {
  94.       console.error(error);
  95.       callback(new Error('Couldn\'t fetch the todo item.'));
  96.       return;
  97.     }
  98.     else {
  99.       // if (email == data.Item.email && password == data.Item.password) {
  100.       //   var token = jwt.sign({
  101.       //     data: "{id: 1,email:'abc@abc.com', name: 'Huy', role: 'user'}",
  102.       //   }, 'secret', { expiresIn: '1h' });
  103.       //   console.log(token);
  104.       //   var decoded = jwt.verify(token, 'secret');
  105.       //   console.log(decoded);
  106.       // }
  107.       // else console.log("invalid");
  108.       console.log(data.Item);
  109.  
  110.     }
  111.   });
  112. };
  113. // list user
  114. module.exports.listuser = (event, context, callback) => {
  115.   const params = {
  116.     TableName: 'Users',
  117.   };
  118.  
  119.   // fetch all todos from the database
  120.   docClient.scan(params, (error, result) => {
  121.     // handle potential errors
  122.     if (error) {
  123.       console.error(error);
  124.       callback(new Error('Couldn\'t fetch the todos.'));
  125.       return;
  126.     }
  127.     const response = {
  128.       statusCode: 200,
  129.       body: JSON.stringify(data.Item),
  130.     };
  131.     callback(null, response);
  132.   });
  133. }
  134.  
  135. // create new table : Movies
  136. module.exports.createtable = (event, context, callback) => {
  137.  
  138.   var params = {
  139.     TableName: "Users",
  140.     KeySchema: [
  141.       { AttributeName: "email", KeyType: "HASH" },  //Partition key
  142.       { AttributeName: "password", KeyType: "RANGE" },  //Sort key
  143.     ],
  144.     AttributeDefinitions: [
  145.       { AttributeName: "email", AttributeType: "S" },  //Partition key
  146.       { AttributeName: "password", AttributeType: "S" },  //Sort key
  147.     ],
  148.     ProvisionedThroughput: {
  149.       ReadCapacityUnits: 1,
  150.       WriteCapacityUnits: 1
  151.     }
  152.   };
  153.  
  154.   dynamodb.createTable(params, function (err, data) {
  155.     if (err) {
  156.       console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
  157.     } else {
  158.       console.log("Created table. Table description JSON:", JSON.stringify(data));
  159.     }
  160.   });
  161.  
  162. };
  163. // add item into table :Users
  164.  
  165. module.exports.addItem = (event, context, cb) => {
  166.   var table = "Users";
  167.  
  168.   var email = "abc1@abc.com";
  169.   var password = "12346";
  170.  
  171.   var itemuser = {
  172.     TableName: table,
  173.     Item: {
  174.       "email": email,
  175.       "password": password,
  176.     }
  177.   };
  178.   console.log("Added Successful");
  179.   return dynamodb.put(itemuser, cb);
  180. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement