summary refs log tree commit diff
path: root/accounts.js
diff options
context:
space:
mode:
Diffstat (limited to 'accounts.js')
-rw-r--r--accounts.js31
1 files changed, 31 insertions, 0 deletions
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