Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- var AWS = require("aws-sdk");
- var jwt = require('jsonwebtoken');
- var dynamodb = new AWS.DynamoDB.DocumentClient({
- region: 'localhost',
- endpoint: 'http://localhost:8000'
- });
- const generatePolicy = function (principalId, effect, resource) {
- const authResponse = {};
- authResponse.principalId = principalId;
- if (effect && resource) {
- const policyDocument = {};
- policyDocument.Version = '2012-10-17';
- policyDocument.Statement = [];
- const statementOne = {};
- statementOne.Action = 'execute-api:Invoke';
- statementOne.Effect = effect;
- statementOne.Resource = resource;
- policyDocument.Statement[0] = statementOne;
- authResponse.policyDocument = policyDocument;
- }
- return authResponse;
- };
- module.exports.hello = (event, context, callback) => {
- const response = {
- statusCode: 200,
- body: JSON.stringify({
- message: 'Your function Hello executed successfully!',
- //input: event,
- }),
- };
- callback(null, response);
- };
- //authorizer
- module.exports.auth = (event, context, callback) => {
- console.log('loading function');
- console.log(event);
- console.log(context);
- var token = event.authorizationToken;
- console.log(token);
- if (token == 'valid') {
- callback(null, generatePolicy('user', 'Allow', event.methodArn));
- } else {
- callback('Unauthorized');
- }
- };
- // get user
- module.exports.getUser = (event, context, callback) => {
- var params = {
- TableName: 'Users',
- Key: {
- email: "abc1@abc.com",
- password: "12346"
- },
- };
- dynamodb.get(params, function (err, data) {
- if (err) {
- console.error(error);
- callback(new Error('Couldn\'t fetch the todo item.'));
- return;
- }
- else {
- const response = {
- statusCode: 200,
- body: JSON.stringify(data),
- };
- callback(null, response);
- }
- });
- }
- //log in user
- module.exports.login = (event, context, callback) => {
- const dataBody = JSON.parse(event.body);
- var email = dataBody.email;
- var password = dataBody.password;
- var params = {
- TableName: 'Users',
- Key: {
- email: dataBody.email,
- password: dataBody.password
- },
- };
- dynamodb.get(params, function (err, data) {
- if (err) {
- console.error(error);
- callback(new Error('Couldn\'t fetch the todo item.'));
- return;
- }
- else {
- // if (email == data.Item.email && password == data.Item.password) {
- // var token = jwt.sign({
- // data: "{id: 1,email:'abc@abc.com', name: 'Huy', role: 'user'}",
- // }, 'secret', { expiresIn: '1h' });
- // console.log(token);
- // var decoded = jwt.verify(token, 'secret');
- // console.log(decoded);
- // }
- // else console.log("invalid");
- console.log(data.Item);
- }
- });
- };
- // list user
- module.exports.listuser = (event, context, callback) => {
- const params = {
- TableName: 'Users',
- };
- // fetch all todos from the database
- docClient.scan(params, (error, result) => {
- // handle potential errors
- if (error) {
- console.error(error);
- callback(new Error('Couldn\'t fetch the todos.'));
- return;
- }
- const response = {
- statusCode: 200,
- body: JSON.stringify(data.Item),
- };
- callback(null, response);
- });
- }
- // create new table : Movies
- module.exports.createtable = (event, context, callback) => {
- var params = {
- TableName: "Users",
- KeySchema: [
- { AttributeName: "email", KeyType: "HASH" }, //Partition key
- { AttributeName: "password", KeyType: "RANGE" }, //Sort key
- ],
- AttributeDefinitions: [
- { AttributeName: "email", AttributeType: "S" }, //Partition key
- { AttributeName: "password", AttributeType: "S" }, //Sort key
- ],
- ProvisionedThroughput: {
- ReadCapacityUnits: 1,
- WriteCapacityUnits: 1
- }
- };
- dynamodb.createTable(params, function (err, data) {
- if (err) {
- console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
- } else {
- console.log("Created table. Table description JSON:", JSON.stringify(data));
- }
- });
- };
- // add item into table :Users
- module.exports.addItem = (event, context, cb) => {
- var table = "Users";
- var email = "abc1@abc.com";
- var password = "12346";
- var itemuser = {
- TableName: table,
- Item: {
- "email": email,
- "password": password,
- }
- };
- console.log("Added Successful");
- return dynamodb.put(itemuser, cb);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement