2024-10-04 18:43:08 -04:00
|
|
|
const express = require('express');
|
|
|
|
const app = express.Router();
|
|
|
|
|
|
|
|
const sqlite = require('better-sqlite3');
|
2024-10-05 12:01:19 -04:00
|
|
|
const db = new sqlite('the_big_db.db', { verbose: console.log });
|
2024-10-04 18:43:08 -04:00
|
|
|
|
|
|
|
const argon2 = require('argon2');
|
|
|
|
|
|
|
|
const { loginRequired } = require('../authStuff.js');
|
|
|
|
|
2024-10-26 15:40:10 -04:00
|
|
|
const { setAttribute } = require('../interpreter.js');
|
|
|
|
|
2024-10-04 18:43:08 -04:00
|
|
|
// auth stuff
|
|
|
|
app.post('/register', async (req, res) => {
|
|
|
|
const {name, password, nonce} = req.body;
|
|
|
|
|
|
|
|
const oldUser = db.prepare('select name from users where name=?').get(name);
|
|
|
|
if (oldUser) return res.status(500).json({"error": "user name already in use"});
|
|
|
|
|
|
|
|
// check if the nonce password is correctt
|
|
|
|
if (nonce != "a softer birdsong") return res.status(500).json({"error": "wrong nonce"});
|
|
|
|
|
|
|
|
try {
|
|
|
|
// i'm told argon2 is the good one nowatimes
|
|
|
|
const hash = await argon2.hash(password);
|
|
|
|
const inserted = db.prepare('insert into users (name, password) values (?, ?)').run(name, hash);
|
|
|
|
res.status(200).json(inserted);
|
2024-10-25 17:48:12 -04:00
|
|
|
// todo: create an object associated with that players
|
2024-10-04 18:43:08 -04:00
|
|
|
} catch (error) {
|
|
|
|
res.status(500).json({"error": error});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
app.post('/login', async (req, res) => {
|
2024-10-08 23:24:48 -04:00
|
|
|
console.log(req.body);
|
2024-10-04 18:43:08 -04:00
|
|
|
if (req.session.name) {
|
|
|
|
return res.status(200).json({message: "already logged in", name: req.session.name});
|
|
|
|
}
|
|
|
|
|
|
|
|
const {name, password} = req.body;
|
|
|
|
|
|
|
|
// fetch username and passswords from the db
|
2024-10-05 12:01:19 -04:00
|
|
|
const storedUser = db.prepare('select * from users where name = ?').get(name);
|
2024-10-04 18:43:08 -04:00
|
|
|
if (!storedUser) {
|
|
|
|
return res.status(401).json({"error": "password/username combo not found in database"});
|
|
|
|
}
|
|
|
|
|
|
|
|
//check if the passss hashes mattch and log in
|
|
|
|
if (!(await argon2.verify(storedUser.password, password))) {
|
|
|
|
return res.status(401).json({"error": "password/username combo not found in database"});
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the session cookie and rreturn 200!
|
|
|
|
req.session.name = name;
|
2024-10-05 12:01:19 -04:00
|
|
|
req.session.userId = storedUser.id;
|
2024-10-21 11:56:48 -04:00
|
|
|
req.session.characterId = db.prepare('select character from users where id=?').get(req.session.userId)?.character;
|
|
|
|
|
2024-10-04 18:43:08 -04:00
|
|
|
console.log('setting req.session.name! : ', req.session);
|
2024-10-05 12:01:19 -04:00
|
|
|
return res.status(200).json({message: "successfully logged in!", id: storedUser.id, name: name});
|
2024-10-04 18:43:08 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
app.post('/logout', (req, res) => {
|
|
|
|
req.session.destroy();
|
|
|
|
res.status(200).json({message: "successfully logged out"});
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/user', loginRequired, (req, res) => {
|
|
|
|
res.status(200).json({
|
2024-10-05 12:01:19 -04:00
|
|
|
"id": req.session.userId,
|
2024-10-04 18:43:08 -04:00
|
|
|
"name": req.session.name,
|
|
|
|
"favoriteColor": "red",
|
|
|
|
"leastFavoriteColor": "also red"
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = app;
|