From 872cb30cee668700dbeb6e540be7cb3d9c319ead Mon Sep 17 00:00:00 2001 From: florian Date: Sat, 6 Sep 2025 00:27:38 +0200 Subject: [PATCH] small change --- index.js | 16 ++++++---------- package-lock.json | 29 +++++++++++++++++++++++++++++ package.json | 1 + 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index bd4ee26..6a0f8a0 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,7 @@ import nodemailer from 'nodemailer'; import dotenv from 'dotenv'; import fs, { stat } from 'fs'; import jwt from 'jsonwebtoken'; +import cookieParser from 'cookie-parser'; dotenv.config(); @@ -32,7 +33,8 @@ function sendMail(to, subject, html) { .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); jwt.verify(token, process.env.JWT_SECRET, (err, user) => { @@ -80,6 +82,7 @@ const port = 20909; app.use(express.json()); app.use(express.static('public')); +app.use(cookieParser()); app.get('/login', (req, res) => { res.sendFile('index.html', { root: 'public' }); @@ -96,15 +99,8 @@ app.get('/', (req, res) => { ); // Exemple d'utilisation : -app.get('/api/loginToken', async (req, res) => { - - 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.get('/api/loginToken', authenticateToken, (req, res) => { + res.status(200).json({ user: req.user }); }); app.post('/api/login', async (req, res) => { diff --git a/package-lock.json b/package-lock.json index b0a969f..72440c6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "cookie-parser": "^1.4.7", "dotenv": "^16.5.0", "jsonwebtoken": "^9.0.2", "nodemailer": "^7.0.3", @@ -104,6 +105,34 @@ "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==", "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": { "version": "4.4.1", "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.1.tgz", diff --git a/package.json b/package.json index d26fbd0..1f2b3ce 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "run": "node index.js" }, "dependencies": { + "cookie-parser": "^1.4.7", "dotenv": "^16.5.0", "jsonwebtoken": "^9.0.2", "nodemailer": "^7.0.3",