Added API endpoints and dockerfile

This commit is contained in:
Lukian 2025-03-24 10:17:37 +01:00
parent 924e55b4f3
commit 5ddad2ed61
31 changed files with 3885 additions and 942 deletions

24
back/api/connect.js Normal file
View file

@ -0,0 +1,24 @@
const express = require('express');
const sha256 = require("sha256");
const jwt = require('jsonwebtoken');
const { getConnection, getUserByUsername } = require('../libs/mysql');
const router = express.Router();
router.post('/', async (req, res) => {
const { username, password } = req.body;
const connection = await getConnection();
const users = await getUserByUsername(connection, username);
connection.end();
if (users[0]) {
if (users[0].password === sha256(password)) {
const token = jwt.sign({ id: users[0].id }, process.env.JWT_SECRET, {
expiresIn: 1000 * 60 * 60 * 24 * 7,
});
res.send({ token: token });
}
}
res.status(401).send({ error: 'Invalid username or password' });
});
module.exports = router;