Skip to content

Commit

Permalink
Merge pull request #101 from lahirumenik/main
Browse files Browse the repository at this point in the history
join game auth logic
  • Loading branch information
lahirumenik authored Jan 24, 2024
2 parents ed2b609 + c365bee commit 6bb102e
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions backend/serverless/Lambda/joingame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@

const { DynamoDBClient, GetItemCommand } = require("@aws-sdk/client-dynamodb");
const client = new DynamoDBClient()


exports.handler = async (event, context, callback) => {
try {


const requestBody = JSON.parse(event.body);
const email = requestBody.email;

const anchorid = requestBody.anchorid;


// Add user to DynamoDB arcore
const dynamoDBParams = {
"TableName": 'Arcombat-arcore',
"Key": {
"email": {"S": email},

}
};
const dynamoDBParamsEnv = {
"TableName": 'Arcombat-env',
"Key": {
"email": {"S": email},

}
};

const commandEnv = new GetItemCommand(dynamoDBParamsEnv);
const responseEnv = await client.send(commandEnv);

const users = responseEnv.Item.users.L;
var found = false
for (let i = 0; i < users.length; i++) {
let value = users[i]["S"];
if (value == email) {
found = true;
break;
}

}
if (found == false) {
return {
statusCode: 500,
body: JSON.stringify('user not in game')
};
}
else{

const command = new GetItemCommand(dynamoDBParams);
const response = await client.send(command);
const anchorid = response.Item.anchorid.S;


// Return a response
return {
statusCode: 200,
body: anchorid
};


}
//error handleing
} catch (error) {
console.error('Error:', error);
return {
statusCode: 500,
body: {"msg":JSON.stringify('Internal Server Error')}
};
}
};

0 comments on commit 6bb102e

Please sign in to comment.