WOSP-APP/serwer/app/apiRoutes.js

66 lines
2.1 KiB
JavaScript
Raw Normal View History

2025-01-01 17:30:11 +00:00
const express = require('express');
2025-01-08 06:48:06 +00:00
const api = express.Router();
2025-01-01 17:30:11 +00:00
const fs = require('fs');
require('dotenv').config();
//mysql
var mysql = require('mysql2');
var con = mysql.createConnection({
host: process.env.MYSQLHOST,
user: process.env.MYSQLUSER,
password: process.env.MYSQLPASS,
port : process.env.MYSQLPORT,
database: process.env.MYSQLDB,
insecureAuth : true
});
con.connect(function(err) {
if (err) throw err;
console.log('Connected!');
});
var bodyParser = require('body-parser');
2025-01-08 06:48:06 +00:00
const {makeid} = require('./func.js');
api.use(bodyParser.json());
2025-01-01 17:30:11 +00:00
2025-01-08 06:48:06 +00:00
api.post("/loginPanel", function(req, res) {
2025-01-01 17:30:11 +00:00
var login = req.body.login;
var password = req.body.password;
2025-01-08 07:46:26 +00:00
con.query("SELECT * FROM login WHERE login = ? AND haslo = SHA1(?)", [login, password], function(err, result) {
2025-01-08 06:48:06 +00:00
//jeżeli znajdziesz wygeneruj token i zapamiętaj go w bazie
2025-01-01 17:30:11 +00:00
if (result.length > 0) {
2025-01-08 06:48:06 +00:00
var token = makeid(64);
2025-01-08 08:19:04 +00:00
con.query("INSERT INTO tokeny (token, typ, userId) VALUES (?, 1, ?)", [token, result[0].id], function(err, result) {
2025-01-08 06:48:06 +00:00
res.send({response: "Zalogowano", token: token});
2025-01-01 17:30:11 +00:00
});
} else {
2025-01-08 06:48:06 +00:00
res.send(403, {response: "Błędne dane logowania"});
2025-01-01 17:30:11 +00:00
}
});
});
2025-01-08 06:48:06 +00:00
api.post("/loginLiczacy", function(req, res) {
var qr = req.body.qr;
con.query("SELECT * FROM liczacy WHERE qr = ? AND aktywne = 1", [qr], function(err, result) {
if(result.length > 0) {
var token = makeid(64);
2025-01-08 08:19:04 +00:00
con.query("INSERT INTO tokenyLiczacy (token, typ, userId) VALUES (?, 1, ?)", [token, result[0].id], function(err, result) {
2025-01-08 06:48:06 +00:00
res.send({response: "Zalogowano", token: token});
2025-01-01 17:30:11 +00:00
});
} else {
2025-01-08 06:48:06 +00:00
res.send(403, {response: "Błędny kod QR"});
2025-01-01 17:30:11 +00:00
}
});
});
2025-01-08 06:48:06 +00:00
const apiPanel = require('./apiPanel.js');
api.use('/panel', apiPanel);
2025-01-01 17:30:11 +00:00
2025-01-08 06:48:06 +00:00
const apiLiczacy = require('./apiLiczacy.js');
api.use('/liczacy', apiLiczacy);
2025-01-01 17:30:11 +00:00
2025-01-08 06:55:28 +00:00
api.all('*', function(req, res) {
res.send(404, {response: "Nie znaleziono"});
});
2025-01-01 17:30:11 +00:00
2025-01-08 06:48:06 +00:00
module.exports = api;