add admin
This commit is contained in:
parent
b8c05105fa
commit
eeb6dffcf2
26
index.js
26
index.js
@ -5,7 +5,7 @@ import bcrypt from 'bcrypt';
|
|||||||
import nodemailer from 'nodemailer';
|
import nodemailer from 'nodemailer';
|
||||||
import dotenv from 'dotenv';
|
import dotenv from 'dotenv';
|
||||||
import fs, { stat } from 'fs';
|
import fs, { stat } from 'fs';
|
||||||
import { verify } from 'crypto';
|
import jwt from 'jsonwebtoken';
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
@ -32,6 +32,26 @@ function sendMail(to, subject, html) {
|
|||||||
.catch(error => console.error('Error sending email:', error));
|
.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({
|
const db = await open({
|
||||||
filename: './db/database.db',
|
filename: './db/database.db',
|
||||||
driver: sqlite3.Database
|
driver: sqlite3.Database
|
||||||
@ -44,6 +64,7 @@ function initializeDatabase() {
|
|||||||
username TEXT NOT NULL UNIQUE,
|
username TEXT NOT NULL UNIQUE,
|
||||||
email TEXT NOT NULL UNIQUE,
|
email TEXT NOT NULL UNIQUE,
|
||||||
password TEXT NOT NULL,
|
password TEXT NOT NULL,
|
||||||
|
admin BOOLEAN DEFAULT 0,
|
||||||
historyToDefault INTEGER 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('INSERT INTO users (username, email, password) VALUES (?, ?, ?)', [verify.username, verify.email, verify.password])
|
||||||
|
|
||||||
await db.run('DELETE FROM verify WHERE id = ?', [verify.id]);
|
await db.run('DELETE FROM verify WHERE id = ?', [verify.id]);
|
||||||
|
|
||||||
|
res.status(200).json({ message: 'Account verified successfully' });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Database error:', err);
|
console.error('Database error:', err);
|
||||||
return res.status(500).json({ message: 'Internal server error' });
|
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}`);
|
console.log(`Server is running on localhost:${port}`);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"dotenv": "^16.5.0",
|
"dotenv": "^16.5.0",
|
||||||
|
"jsonwebtoken": "^9.0.2",
|
||||||
"nodemailer": "^7.0.3",
|
"nodemailer": "^7.0.3",
|
||||||
"nodemon": "^3.1.10"
|
"nodemon": "^3.1.10"
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user