zkt26/z1/webapp/routes/inscription.js
2026-03-31 19:33:15 +02:00

45 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const express = require('express');
const router = express.Router();
const bcrypt = require('bcrypt');
const db = require('../config/db');
const multer = require('multer');
const path = require('path');
const storage = multer.diskStorage({
destination: (req, file, cb) => {
const uploadDir = path.join(__dirname, '../img');
cb(null, uploadDir);
},
filename: (req, file, cb) => {
const uniqueName = `${Date.now()}_${file.originalname}`;
cb(null, uniqueName);
}
});
const upload = multer({ storage });
router.get('/', (req, res) => {
res.render('inscription');
});
router.post('/', upload.single('photo_profil'), async (req, res) => {
const { nom, prenom, sexe, age, date_naissance, identifiant, mot_de_passe, situation, email } = req.body;
const photo = req.file ? req.file.filename : null;
const hashedPassword = await bcrypt.hash(mot_de_passe, 10);
const sql = `INSERT INTO utilisateur (nom, prenom, sexe, age, date_naissance, identifiant, mot_de_passe, photo, situation, email)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
db.query(sql, [nom, prenom, sexe, age, date_naissance, identifiant, hashedPassword, photo, situation, email], (err, result) => {
if (err) {
console.error(err);
return res.send('Erreur lors de linscription');
}
res.redirect('/connexion');
});
});
module.exports = router;