Last active
June 27, 2021 04:37
-
-
Save yai333/b71ac664cfbf9fc55650856ddbb47a2d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| '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