mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-13 23:17:02 +03:00
Initial refactor (routes and utils to respective folders)
This commit is contained in:
54
src/routes/setUsername.js
Normal file
54
src/routes/setUsername.js
Normal file
@@ -0,0 +1,54 @@
|
||||
|
||||
var fs = require('fs');
|
||||
var config = JSON.parse(fs.readFileSync('config.json'));
|
||||
|
||||
var db = require('../databases/databases.js').db;
|
||||
var getHash = require('../utils/getHash.js');
|
||||
|
||||
|
||||
module.exports = function setUsername(req, res) {
|
||||
let userID = req.query.userID;
|
||||
let userName = req.query.username;
|
||||
|
||||
let adminUserIDInput = req.query.adminUserID;
|
||||
|
||||
if (userID == undefined || userName == undefined || userID === "undefined") {
|
||||
//invalid request
|
||||
res.sendStatus(400);
|
||||
return;
|
||||
}
|
||||
|
||||
if (adminUserIDInput != undefined) {
|
||||
//this is the admin controlling the other users account, don't hash the controling account's ID
|
||||
adminUserIDInput = getHash(adminUserIDInput);
|
||||
|
||||
if (adminUserIDInput != config.adminUserID) {
|
||||
//they aren't the admin
|
||||
res.sendStatus(403);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
//hash the userID
|
||||
userID = getHash(userID);
|
||||
}
|
||||
|
||||
try {
|
||||
//check if username is already set
|
||||
let row = db.prepare("SELECT count(*) as count FROM userNames WHERE userID = ?").get(userID);
|
||||
|
||||
if (row.count > 0) {
|
||||
//already exists, update this row
|
||||
db.prepare("UPDATE userNames SET userName = ? WHERE userID = ?").run(userName, userID);
|
||||
} else {
|
||||
//add to the db
|
||||
db.prepare("INSERT INTO userNames VALUES(?, ?)").run(userID, userName);
|
||||
}
|
||||
|
||||
res.sendStatus(200);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
res.sendStatus(500);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user