From 4079419fa8998a49117a4bfd3f7501ab14be6f7a Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Mon, 12 Aug 2019 23:31:44 -0400 Subject: [PATCH] Added API endpoint to get the username --- index.js | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 4ba31cf..9a34b97 100644 --- a/index.js +++ b/index.js @@ -252,7 +252,7 @@ app.get('/api/viewedVideoSponsorTime', function (req, res) { }); //To set your username for the stats view -app.get('/api/setUsername', function (req, res) { +app.post('/api/setUsername', function (req, res) { let userID = req.query.userID; let userName = req.query.username; @@ -281,6 +281,35 @@ app.get('/api/setUsername', function (req, res) { }); }); +//get what username this user has +app.get('/api/getUsername', function (req, res) { + let userID = req.query.userID; + + if (userID == undefined) { + //invalid request + res.sendStatus(400); + return; + } + + //hash the userID + userID = getHash(userID); + + db.prepare("SELECT userName FROM userNames WHERE userID = ?").get(userID, function(err, row) { + if (err) console.log(err); + + if (row != null) { + res.send({ + userName: row.userName + }); + } else { + //no username yet, just send back the userID + res.send({ + userName: userID + }); + } + }); +}); + //Gets all the views added up for one userID //Useful to see how much one user has contributed app.get('/api/getViewsForUser', function (req, res) {