From f175fd8c98906a07ce20936a33bc30e26763f68d Mon Sep 17 00:00:00 2001 From: Liam <67254223+RealFascinated@users.noreply.github.com> Date: Wed, 19 Oct 2022 17:15:24 +0100 Subject: [PATCH] Added steam id validator --- pages/api/isvalidsteamid.js | 47 +++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 pages/api/isvalidsteamid.js diff --git a/pages/api/isvalidsteamid.js b/pages/api/isvalidsteamid.js new file mode 100644 index 0000000..a17bd48 --- /dev/null +++ b/pages/api/isvalidsteamid.js @@ -0,0 +1,47 @@ +import WebsiteTypes from "../../src/consts/WebsiteType"; + +const TO_CHECK = [ + WebsiteTypes.ScoreSaber.ApiUrl, + WebsiteTypes.BeatLeader.ApiUrl, +]; + +// TODO: Cache the result +export default async function handler(req, res) { + const steamId = req.query.steamid; + if (!steamId) { + return res.json({ + status: 404, + message: "Steam id not provided: Provide in the query.", + }); + } + + let invalid = false; + for (const url of TO_CHECK) { + const isValid = await checkLeaderboard(url, steamId); + + if (isValid) { + break; + } + if (!isValid) { + invalid = true; + break; + } + } + + return res.json({ + status: "OK", + message: invalid ? `Invalid` : "Valid", + }); +} + +async function checkLeaderboard(url, steamId) { + console.log(url.replace("%s", steamId)); + const data = await fetch(url.replace("%s", steamId), { + headers: { + "X-Requested-With": "BeatSaber Overlay", + }, + }); + const json = await data.json(); + + return !!json.pp; +}