small change
This commit is contained in:
parent
ad29e8db5f
commit
872cb30cee
16
index.js
16
index.js
@ -6,6 +6,7 @@ import nodemailer from 'nodemailer';
|
|||||||
import dotenv from 'dotenv';
|
import dotenv from 'dotenv';
|
||||||
import fs, { stat } from 'fs';
|
import fs, { stat } from 'fs';
|
||||||
import jwt from 'jsonwebtoken';
|
import jwt from 'jsonwebtoken';
|
||||||
|
import cookieParser from 'cookie-parser';
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
@ -32,7 +33,8 @@ function sendMail(to, subject, html) {
|
|||||||
.catch(error => console.error('Error sending email:', error));
|
.catch(error => console.error('Error sending email:', error));
|
||||||
}
|
}
|
||||||
|
|
||||||
function authenticateToken(token) {
|
function authenticateToken(req, res, next) {
|
||||||
|
const token = req.cookies.auth_token;
|
||||||
if (!token) return res.sendStatus(401);
|
if (!token) return res.sendStatus(401);
|
||||||
|
|
||||||
jwt.verify(token, process.env.JWT_SECRET, (err, user) => {
|
jwt.verify(token, process.env.JWT_SECRET, (err, user) => {
|
||||||
@ -80,6 +82,7 @@ const port = 20909;
|
|||||||
|
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
app.use(express.static('public'));
|
app.use(express.static('public'));
|
||||||
|
app.use(cookieParser());
|
||||||
|
|
||||||
app.get('/login', (req, res) => {
|
app.get('/login', (req, res) => {
|
||||||
res.sendFile('index.html', { root: 'public' });
|
res.sendFile('index.html', { root: 'public' });
|
||||||
@ -96,15 +99,8 @@ app.get('/', (req, res) => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Exemple d'utilisation :
|
// Exemple d'utilisation :
|
||||||
app.get('/api/loginToken', async (req, res) => {
|
app.get('/api/loginToken', authenticateToken, (req, res) => {
|
||||||
|
res.status(200).json({ user: req.user });
|
||||||
if (!req.cookies) return res.sendStatus(401);
|
|
||||||
|
|
||||||
if (!req.cookies.auth_token) return res.sendStatus(401);
|
|
||||||
|
|
||||||
const token = req.cookies.auth_token;
|
|
||||||
|
|
||||||
authenticateToken(token);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post('/api/login', async (req, res) => {
|
app.post('/api/login', async (req, res) => {
|
||||||
|
29
package-lock.json
generated
29
package-lock.json
generated
@ -9,6 +9,7 @@
|
|||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"cookie-parser": "^1.4.7",
|
||||||
"dotenv": "^16.5.0",
|
"dotenv": "^16.5.0",
|
||||||
"jsonwebtoken": "^9.0.2",
|
"jsonwebtoken": "^9.0.2",
|
||||||
"nodemailer": "^7.0.3",
|
"nodemailer": "^7.0.3",
|
||||||
@ -104,6 +105,34 @@
|
|||||||
"integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==",
|
"integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/cookie": {
|
||||||
|
"version": "0.7.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/cookie/-/cookie-0.7.2.tgz",
|
||||||
|
"integrity": "sha512-yki5XnKuf750l50uGTllt6kKILY4nQ1eNIQatoXEByZ5dWgnKqbnqmTrBE5B4N7lrMJKQ2ytWMiTO2o0v6Ew/w==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/cookie-parser": {
|
||||||
|
"version": "1.4.7",
|
||||||
|
"resolved": "https://registry.npmjs.org/cookie-parser/-/cookie-parser-1.4.7.tgz",
|
||||||
|
"integrity": "sha512-nGUvgXnotP3BsjiLX2ypbQnWoGUPIIfHQNZkkC668ntrzGWEZVW70HDEB1qnNGMicPje6EttlIgzo51YSwNQGw==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"cookie": "0.7.2",
|
||||||
|
"cookie-signature": "1.0.6"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.8.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/cookie-signature": {
|
||||||
|
"version": "1.0.6",
|
||||||
|
"resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz",
|
||||||
|
"integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/debug": {
|
"node_modules/debug": {
|
||||||
"version": "4.4.1",
|
"version": "4.4.1",
|
||||||
"resolved": "https://registry.npmjs.org/debug/-/debug-4.4.1.tgz",
|
"resolved": "https://registry.npmjs.org/debug/-/debug-4.4.1.tgz",
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
"run": "node index.js"
|
"run": "node index.js"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"cookie-parser": "^1.4.7",
|
||||||
"dotenv": "^16.5.0",
|
"dotenv": "^16.5.0",
|
||||||
"jsonwebtoken": "^9.0.2",
|
"jsonwebtoken": "^9.0.2",
|
||||||
"nodemailer": "^7.0.3",
|
"nodemailer": "^7.0.3",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user