From 7c9edccaa58a1e10c349469bccc44f0834cf4e2f Mon Sep 17 00:00:00 2001 From: WlodekM Date: Wed, 3 Jul 2024 16:39:35 +0300 Subject: auth and :jsonGet --- accounts.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 accounts.js (limited to 'accounts.js') diff --git a/accounts.js b/accounts.js new file mode 100644 index 0000000..f45ab6e --- /dev/null +++ b/accounts.js @@ -0,0 +1,31 @@ +import { createHash } from "crypto" +import cuid from 'cuid'; +import fs from "fs" + +if(!fs.existsSync("db")) fs.mkdirSync("db"); +if(!fs.existsSync("db/users.json")) fs.writeFileSync("db/users.json", "{}"); + +const db = JSON.parse(fs.readFileSync("db/users.json").toString()) + +function syncDB() { + fs.writeFileSync("db/users.json", JSON.stringify(db)) +} + +export function checkAccount(username) { + return db[username] != undefined +} + +export function createAccount(username, password) { + let hashedPassword = createHash('sha256').update(password).digest('hex'); + db[username] = { + id: cuid(), + password: hashedPassword, + t: Number(new Date()) / 1000 + }; + syncDB(); +} + +export function checkPassword(username, password) { + let hashedPassword = createHash('sha256').update(password).digest('hex'); + return db[username]?.password === hashedPassword +} \ No newline at end of file -- cgit 1.4.1-2-gfad0