Skip to content

Instantly share code, notes, and snippets.

@yai333
Last active June 27, 2021 04:37
Show Gist options
  • Select an option

  • Save yai333/b71ac664cfbf9fc55650856ddbb47a2d to your computer and use it in GitHub Desktop.

Select an option

Save yai333/b71ac664cfbf9fc55650856ddbb47a2d to your computer and use it in GitHub Desktop.
'use strict';
const AWS = require('aws-sdk');
const dynamo = new AWS.DynamoDB();
const headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
};
module.exports.handler = async (event, context, callback) => {
const {
queryStringParameters: { address },
} = event;
try {
const { Items: nonces } = await getNonce(address);
if (nonces && nonces.length > 0) {
return {
headers,
statusCode: 200,
body: JSON.stringify(AWS.DynamoDB.Converter.unmarshall(nonces[0])),
};
}
return {
headers,
statusCode: 404,
body: JSON.stringify({
nonce: null,
}),
};
} catch (error) {
return callback(err, null);
}
};
const getNonce = (address) => {
const params = {
Statement: `SELECT "nonce"
FROM "${process.env.USERTABLE_NAME}" WHERE "address"='${address}'`,
};
return dynamo.executeStatement(params).promise();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment