diff --git a/index.js b/index.js index a91e524..0e2c67e 100644 --- a/index.js +++ b/index.js @@ -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' }); @@ -202,4 +225,5 @@ app.post('/api/verify', async (req, res) => { app.listen(port, "127.0.0.1", () => { console.log(`Server is running on localhost:${port}`); } -); \ No newline at end of file +); + diff --git a/package.json b/package.json index d477aae..d26fbd0 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ }, "dependencies": { "dotenv": "^16.5.0", + "jsonwebtoken": "^9.0.2", "nodemailer": "^7.0.3", "nodemon": "^3.1.10" }