add admin

This commit is contained in:
florian 2025-06-14 13:17:16 +02:00
parent b8c05105fa
commit eeb6dffcf2
2 changed files with 27 additions and 2 deletions

View File

@ -5,7 +5,7 @@ import bcrypt from 'bcrypt';
import nodemailer from 'nodemailer';
import dotenv from 'dotenv';
import fs, { stat } from 'fs';
import { verify } from 'crypto';
import jwt from 'jsonwebtoken';
dotenv.config();
@ -32,6 +32,26 @@ function sendMail(to, subject, html) {
.catch(error => console.error('Error sending email:', error));
}
// Middleware for checking the token
const verifyToken = (req, res, next) => {
// Get the token from the headers
const token = req.headers.authorization?.replace("Bearer ", "");
// Check if the token is missing
if (!token) {
return res.status(403).json({ error: "Acces unauthorized, token required" });
}
try {
// Decode the token and set the user information in the request
req.user = jwt.verify(token, jwtSecret);
// The token is valid and the user is authorized to access the route
next();
} catch (error) {
return res.status(401).json({ error: "Acces unauthorized, invalid token" });
}
};
const db = await open({
filename: './db/database.db',
driver: sqlite3.Database
@ -44,6 +64,7 @@ function initializeDatabase() {
username TEXT NOT NULL UNIQUE,
email TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
admin BOOLEAN DEFAULT 0,
historyToDefault INTEGER DEFAULT 0
);
`);
@ -192,6 +213,8 @@ app.post('/api/verify', async (req, res) => {
await db.run('INSERT INTO users (username, email, password) VALUES (?, ?, ?)', [verify.username, verify.email, verify.password])
await db.run('DELETE FROM verify WHERE id = ?', [verify.id]);
res.status(200).json({ message: 'Account verified successfully' });
} catch (err) {
console.error('Database error:', err);
return res.status(500).json({ message: 'Internal server error' });
@ -203,3 +226,4 @@ app.listen(port, "127.0.0.1", () => {
console.log(`Server is running on localhost:${port}`);
}
);

View File

@ -16,6 +16,7 @@
},
"dependencies": {
"dotenv": "^16.5.0",
"jsonwebtoken": "^9.0.2",
"nodemailer": "^7.0.3",
"nodemon": "^3.1.10"
}