Commit 42e5f4fd authored by Romain OZDEN's avatar Romain OZDEN

Authentification avec passport marche pas on est super contents

parent f83621f8
......@@ -3,7 +3,7 @@ const { create,
getUserById,
updateUser,
deleteUser,
getUserByEmail,
getUserByUsername,
} = require('./user.service');
const { genSaltSync, hashSync, compareSync } = require('bcrypt');
......@@ -15,13 +15,16 @@ module.exports = {
const salt = genSaltSync(10);
body.user.password = hashSync(body.user.password, salt);
console.log(body.user.password, 'ça a l\'air de marcher');
create(body, (err, results) => {
if (err) {
create(body, results => {
if (results === 0) {
console.log(err);
return res.status(500).json({
success: 0,
message: 'Database connection error'
message: 'Error'
});
} else {
console.log(results);
return res.redirect('/login');
}
});
},
......@@ -45,13 +48,8 @@ module.exports = {
});
},
getUsers: (req, res) => {
getUsers((err, results) => {
if (err) {
console.log(err);
return;
}
getUsers((results) => {
return res.json({
success: 1,
data: results
});
})
......@@ -96,35 +94,4 @@ module.exports = {
});
});
},
login: (req,res) => {
const body = req.body;
getUserByEmail(body.email, (err, results) => {
if (err) {
console.log(err);
}
if (!results) {
return res.json({
success: 0,
data: 'Invalid email or password'
});
}
const result = compareSync(body.password, results.password);
if (result) {
results.password = undefined;
const jsontoken = sign({ result: results }, 'qwe1234', {
expiresIn : '1h'
});
return res.json({
success: 1,
message: 'login successfully',
token : jsontoken
});
} else {
return res.json({
success: 0,
data: 'Invalied email or password'
});
}
});
}
}
\ No newline at end of file
......@@ -3,16 +3,41 @@ const { createUser,
getUserById,
updateUser,
deleteUser,
login,
} = require('./user.controller');
const router = require('express').Router();
const { checkToken } = require('../../auth/token_validation');
const sec = require('connect-ensure-login');
router.post('/add', createUser);
router.get('/', getUsers);
router.get('/:id', checkToken, getUserById);
router.patch('/', checkToken, updateUser);
router.delete('/', checkToken, deleteUser);
router.post('/login', login);
const {getUserByUsername} = require('./user.service');
module.exports = router;
\ No newline at end of file
module.exports = (passport) => {
const router = require('express').Router();
router.post('/add', createUser);
router.get('/', sec.ensureLoggedIn(), getUsers);
router.get('/:id', getUserById);
router.patch('/', updateUser);
router.delete('/', deleteUser);
router.post('/login', (req,res,next) => {
const body = req.body;
getUserByUsername(body.username, results => {
if (!results) {
return res.json({ success: 0, data: 'User not found' });
}
passport.authenticate('local', function (err, user) {
if (err) {
return next(err); // will generate a 500 error
}
if (!user) {
return res.send({succes: false, message: 'authentication failed'});
}
req.login(user, function (err) {
if (err) {
return next(err);
}
return res.send({success: true, message: 'authentication succeeded'});
});
})(req, res, next);
});
});
return router;
}
\ No newline at end of file
......@@ -6,37 +6,45 @@ ATTACH FILESTORAGE DATABASE annales("${__dirname}/../../db/db.json");
USE annales;
`);
function getMaxUserId() {
let id;
id = allah.exec(`select user_id from Utilisateur ORDER BY user_id DESC`)[0].user_id + 1;
if (!id) {
id = 1;
}
return id;
}
module.exports = {
create: (data, callBack) => {
allah.exec(
`insert into Utilisateur(username, password, firstname, lastname, email)
values(?,?,?,?,?)`,
`insert into Utilisateur(user_id, username, password, firstname, lastname, email, role)
values(?,?,?,?,?,?,?)`,
[
getMaxUserId(),
data.user.username,
data.user.password,
data.user.firstname,
data.user.lastname,
data.user.email,
data.user.role === 'admin' ? 'admin' : 'etudiant',
],
(res) => {
console.log(res);
callBack(res);
}
);
},
getUsers: callBack => {
sqlcon.query(
`select id, username, password, firstname, lastname, email from utilisateur`,
allah.exec(
`select * from Utilisateur`,
[],
(error, results, fields) => {
if (error) {
return callBack(error);
}
return callBack(null, results);
(results) => {
return callBack(results);
}
);
},
getUserById : (id, callBack) => {
sqlcon.query(
allah.exec(
`select id, nom, prenom, username, password, email from utilisateur where id = ?`,
[id],
(error, results, fields) => {
......@@ -48,7 +56,7 @@ module.exports = {
)
},
updateUser: (data, callBack) => {
sqlcon.query(
allah.exec(
`update utilisateur set nom=?, prenom=?, username=?, password=?, email=? where id = ?`,
[
data.nom,
......@@ -67,7 +75,7 @@ module.exports = {
);
},
deleteUser: (data, callBack) => {
sqlcon.query(
allah.exec(
`delete from utilisateur where id = ?`,
[data.id],
(error, results, fields) => {
......@@ -78,15 +86,13 @@ module.exports = {
}
)
},
getUserByEmail: (email, callback) => {
sqlcon.query(
`select * from utilisateur where email = ?`,
[email],
(error, results, fields) => {
if (error) {
callBack(error);
}
return callBack(null, results[0]);
getUserByUsername: (username, callBack) => {
console.log(username);
allah.exec(
`select * from Utilisateur where username = ?`,
[username],
(res) => {
return callBack(res[0].username);
}
);
}
......
/* eslint-env node */
'use strict';
const {getUserByUsername} = require('../api/users/user.service');
// Ce module permet de gérer l'authentification avec la librairie passportjs
// Il dépend également du module dbHelper puisque les informations de nos
// utilisateurs sont stockées dans la base de données
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
// LocalStrategy = stockage des identifiants et mots de passe
// des utilisateurs en local dans notre base de données
passport.use(new LocalStrategy(
function (username, password, cb) {
// On récupère les information (mot de passe) de l'utilisateur
// passé en paramètre
getUserByUsername(username, (user) => {
if (!user) {
cb(null, false);
}
// Utilisateur dans la base de données et mot de passe ok
else if (user.password === password) {
cb(null, user);
}
// Utilisateur dans la base de données mais mauvais mot de passe
else {
cb(null, false);
}
});
}
));
// Stocke les données de l'utilisation dans le cookie de session
passport.serializeUser(function (user, cb) {
console.debug('serializeUser ', JSON.stringify(user));
cb(null, user.id);
});
// Récupère les données de l'utilisateur depuis le cookie de session
passport.deserializeUser(function (id, cb) {
console.debug('deserializeUser ' + id);
});
// Puisque c'est un module, on export au moins une fonction
// Ici c'est un "constructeur" qui prend une application express
// en paramètre afin de se déclarer comme middlewwre et pouvoir gérer
// l'authentification sur toutes les routes du site
module.exports = function (app) {
app.use(require('cookie-parser')());
app.use(require('body-parser').urlencoded({extended: true}));
app.use(require('express-session')({secret: 'keyboard cat', resave: false, saveUninitialized: false}));
// Initialize Passport and restore authentication state, if any, from the
// session.
app.use(passport.initialize());
app.use(passport.session());
return passport;
};
const { verify } = require('jsonwebtoken');
module.exports = {
checkToken: (req, res, next) => {
let token = req.get('authorization');
if (token) {
token = token.slice(7);
verify(token, 'qwe1234', (err, decoded) => {
if (err){
res.json({
success: 0,
message: 'Invalid token'
})
} else {
next();
}
})
} else {
res.json({
success: 0,
message: 'Access denied! Unauthorized user'
});
}
}
}
\ No newline at end of file
{"tables":{"Promo":{"columns":[{"columnid":"promo_id","dbtypeid":"NUMBER UNSIGNED","notnull":true,"identity":{"value":1,"step":1}},{"columnid":"annee","dbtypeid":"STRING"},{"columnid":"specialite","dbtypeid":"STRING"}]},"Role":{"columns":[{"columnid":"role_id","dbtypeid":"NUMBER UNSIGNED","notnull":true,"identity":{"value":1,"step":1}},{"columnid":"nom","dbtypeid":"STRING"}],"dirty":true},"Utilisateur":{"columns":[{"columnid":"user_id","dbtypeid":"NUMBER UNSIGNED","notnull":true,"identity":{"value":1,"step":1}},{"columnid":"identifiant","dbtypeid":"STRING","notnull":true},{"columnid":"mot_de_passe","dbtypeid":"STRING","notnull":true},{"columnid":"nom","dbtypeid":"STRING"},{"columnid":"prenom","dbtypeid":"STRING"},{"columnid":"email","dbtypeid":"STRING"},{"columnid":"role_id","dbtypeid":"NUMBER UNSIGNED","notnull":true}],"dirty":true},"Matiere":{"columns":[{"columnid":"matiere_id","dbtypeid":"NUMBER UNSIGNED","notnull":true,"identity":{"value":1,"step":1}},{"columnid":"nom","dbtypeid":"STRING"},{"columnid":"user_id","dbtypeid":"NUMBER UNSIGNED","notnull":true},{"columnid":"promo_id","dbtypeid":"NUMBER UNSIGNED","notnull":true}]},"Sujet":{"columns":[{"columnid":"sujet_id","dbtypeid":"NUMBER UNSIGNED","notnull":true,"identity":{"value":1,"step":1}},{"columnid":"nom","dbtypeid":"STRING"},{"columnid":"pdf_sujet","dbtypeid":"STRING"},{"columnid":"pdf_correction","dbtypeid":"STRING"},{"columnid":"verif","dbtypeid":"BOOLEAN"},{"columnid":"date_depot","dbtypeid":"DATE"},{"columnid":"matiere_id","dbtypeid":"NUMBER UNSIGNED","notnull":true}]},"Commentaire":{"columns":[{"columnid":"com_id","dbtypeid":"NUMBER UNSIGNED","notnull":true,"identity":{"value":1,"step":1}},{"columnid":"position","dbtypeid":"NUMBER UNSIGNED"},{"columnid":"message","dbtypeid":"STRING"},{"columnid":"typesujet","dbtypeid":"STRING"},{"columnid":"sujet_id","dbtypeid":"NUMBER UNSIGNED","notnull":true},{"columnid":"user_id","dbtypeid":"NUMBER UNSIGNED","notnull":true}]},"Notification":{"columns":[{"columnid":"notif_id","dbtypeid":"NUMBER UNSIGNED","notnull":true,"identity":{"value":1,"step":1}},{"columnid":"user_id","dbtypeid":"NUMBER UNSIGNED","notnull":true}]},"Suivre":{"columns":[{"columnid":"matiere_id","dbtypeid":"NUMBER UNSIGNED","notnull":true},{"columnid":"user_id","dbtypeid":"NUMBER UNSIGNED","notnull":true}]}},"Promo":[],"Role":[{"nom":"Etudiant"},{"nom":"Admin"}],"Utilisateur":[{"username":"ihu","password":"$2b$10$PNhFNzA3KuuV.2fKY1CcwuU/7iGgjZeuF1LfoFl.Kjfo9emcYY7aq","email":""},{"username":"ihu","password":"$2b$10$ZUUkLlBmX50KXjoAfKxXXOJZsu..rvnab6F187R1LNhLNqrFvwg2S","firstname":"","lastname":"","email":""},{"username":"romain","password":"$2b$10$BsEwI.UI0dxqQgaNCTP8/u9IGtpPipW1kTK3j/fVFNkcaqfvU9odm","firstname":"","lastname":"","email":""}],"Matiere":[],"Sujet":[],"Commentaire":[],"Notification":[],"Suivre":[]}
\ No newline at end of file
{"tables":{"Promo":{"columns":[{"columnid":"promo_id","dbtypeid":"NUMBER UNSIGNED","notnull":true,"identity":{"value":1,"step":1}},{"columnid":"annee","dbtypeid":"STRING"},{"columnid":"specialite","dbtypeid":"STRING"}]},"Role":{"columns":[{"columnid":"role_id","dbtypeid":"NUMBER UNSIGNED","notnull":true,"identity":{"value":1,"step":1}},{"columnid":"nom","dbtypeid":"STRING"}],"dirty":true},"Utilisateur":{"columns":[{"columnid":"user_id","dbtypeid":"NUMBER UNSIGNED","notnull":true,"identity":{"value":1,"step":1}},{"columnid":"username","dbtypeid":"STRING","notnull":true},{"columnid":"password","dbtypeid":"STRING","notnull":true},{"columnid":"firstname","dbtypeid":"STRING"},{"columnid":"lastname","dbtypeid":"STRING"},{"columnid":"email","dbtypeid":"STRING"},{"columnid":"role_id","dbtypeid":"NUMBER UNSIGNED","notnull":true}],"dirty":true},"Matiere":{"columns":[{"columnid":"matiere_id","dbtypeid":"NUMBER UNSIGNED","notnull":true,"identity":{"value":1,"step":1}},{"columnid":"nom","dbtypeid":"STRING"},{"columnid":"user_id","dbtypeid":"NUMBER UNSIGNED","notnull":true},{"columnid":"promo_id","dbtypeid":"NUMBER UNSIGNED","notnull":true}]},"Sujet":{"columns":[{"columnid":"sujet_id","dbtypeid":"NUMBER UNSIGNED","notnull":true,"identity":{"value":1,"step":1}},{"columnid":"nom","dbtypeid":"STRING"},{"columnid":"pdf_sujet","dbtypeid":"STRING"},{"columnid":"pdf_correction","dbtypeid":"STRING"},{"columnid":"verif","dbtypeid":"BOOLEAN"},{"columnid":"date_depot","dbtypeid":"DATE"},{"columnid":"matiere_id","dbtypeid":"NUMBER UNSIGNED","notnull":true}]},"Commentaire":{"columns":[{"columnid":"com_id","dbtypeid":"NUMBER UNSIGNED","notnull":true,"identity":{"value":1,"step":1}},{"columnid":"position","dbtypeid":"NUMBER UNSIGNED"},{"columnid":"message","dbtypeid":"STRING"},{"columnid":"typesujet","dbtypeid":"STRING"},{"columnid":"sujet_id","dbtypeid":"NUMBER UNSIGNED","notnull":true},{"columnid":"user_id","dbtypeid":"NUMBER UNSIGNED","notnull":true}]},"Notification":{"columns":[{"columnid":"notif_id","dbtypeid":"NUMBER UNSIGNED","notnull":true,"identity":{"value":1,"step":1}},{"columnid":"user_id","dbtypeid":"NUMBER UNSIGNED","notnull":true}]},"Suivre":{"columns":[{"columnid":"matiere_id","dbtypeid":"NUMBER UNSIGNED","notnull":true},{"columnid":"user_id","dbtypeid":"NUMBER UNSIGNED","notnull":true}]}},"Promo":[],"Role":[{"id":1,"nom":"Etudiant"},{"id":2,"nom":"Admin"}],"Utilisateur":[{"username":"romain","password":"$2b$10$KMoSK0svIqmvfsR2fnn04ONEztKsnHwOt7.VpEVucPPM8mnv89PXW","firstname":"","lastname":"","email":""},{"username":"romain","password":"$2b$10$Te4rAuQlw.5w/0TY956J5O4LSi3L1Cb6U93ZAGMicJy8xidzgS/Di","firstname":"","lastname":"","email":""},{"username":"romain","password":"$2b$10$maeAQpSmqzfm8QSy7AX4E.IRKJyjQ465/y0p1Yz0yGqJ8yHrdSKHi","firstname":"","lastname":"","email":""},{"username":"romain","password":"$2b$10$hKtLNTYk72kiEQhVuIDHeuC9xFXYWdVEdV2U.s3MZra/bg4oOs3ju","firstname":"","lastname":"","email":""},{"username":"romain","password":"$2b$10$.JVsqxJOo3Vr5Jz50sTN6.038G2.RICvzpbS8F8Dh/B2frTUSAB16","firstname":"","lastname":"","email":""},{"username":"romain","password":"$2b$10$Er/LPepl1HIva/1rMOmRye.DDlkLP5gu8G4x7b7L6JjMyPKWrmdTi","firstname":"","lastname":"","email":""},{"user_id":1,"username":"$2b$10$ASz.kqALa/Hn1GzwtGYodOOQCtgehDiAK9BHMLrZK1apTEJtrzKdi","password":"","firstname":"","lastname":"","email":"etudiant"},{"user_id":2,"username":"bite","password":"$2b$10$UcJMN4YM7RBRTSH1gwMac.kxMbCenRh/KRcjdI0yHnIaNpANTjM/W","firstname":"","lastname":"","email":"","role":"etudiant"},{"user_id":3,"username":"bite","password":"$2b$10$NKX1o7zHC60VcHWzhq/5fuz2mrwPaMLB8iiXx3z0mTrY0kkUBvswC","firstname":"","lastname":"","email":"","role":"etudiant"}],"Matiere":[],"Sujet":[],"Commentaire":[],"Notification":[],"Suivre":[]}
\ No newline at end of file
......@@ -4,20 +4,14 @@ CREATE TABLE IF NOT EXISTS Promo (
specialite string
);
CREATE TABLE IF NOT EXISTS `Role` (
role_id number UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
nom string UNIQUE
);
CREATE TABLE IF NOT EXISTS Utilisateur (
`user_id` number UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
identifiant string NOT NULL UNIQUE,
mot_de_passe string NOT NULL,
nom string,
prenom string,
username string NOT NULL UNIQUE,
`password` string NOT NULL,
firstname string,
lastname string,
email string,
role_id number UNSIGNED NOT NULL,
CONSTRAINT `fk_user_role` FOREIGN KEY (role_id) REFERENCES Role (role_id)
`role` string NOT NULL,
);
CREATE TABLE IF NOT EXISTS Matiere (
......@@ -64,6 +58,4 @@ CREATE TABLE IF NOT EXISTS `Suivre` (
PRIMARY KEY (matiere_id, `user_id`),
CONSTRAINT `fk_suj_mat` FOREIGN KEY (matiere_id) REFERENCES Matiere (matiere_id),
CONSTRAINT `fk_notif_user` FOREIGN KEY (`user_id`) REFERENCES Utilisateur (`user_id`)
);
INSERT INTO `Role`(nom) VALUES ('Etudiant'), ('Admin');
\ No newline at end of file
);
\ No newline at end of file
......@@ -967,6 +967,11 @@
"xdg-basedir": "^4.0.0"
}
},
"connect-ensure-login": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/connect-ensure-login/-/connect-ensure-login-0.1.1.tgz",
"integrity": "sha1-F03MUSQ7nqwj+NmCFa62aU4uihI="
},
"console-control-strings": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/console-control-strings/-/console-control-strings-1.1.0.tgz",
......@@ -1012,6 +1017,15 @@
"resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.0.tgz",
"integrity": "sha512-+Hp8fLp57wnUSt0tY0tHEXh4voZRDnoIrZPqlo3DPiI4y9lwg/jqx+1Om94/W6ZaPDOUbnjOt/99w66zk+l1Xg=="
},
"cookie-parser": {
"version": "1.4.5",
"resolved": "https://registry.npmjs.org/cookie-parser/-/cookie-parser-1.4.5.tgz",
"integrity": "sha512-f13bPUj/gG/5mDr+xLmSxxDsB9DQiTIfhJS/sqjrmfAWiAN+x2O4i/XguTL9yDZ+/IFDanJ+5x7hC4CXT9Tdzw==",
"requires": {
"cookie": "0.4.0",
"cookie-signature": "1.0.6"
}
},
"cookie-signature": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz",
......@@ -1236,6 +1250,11 @@
"is-obj": "^2.0.0"
}
},
"dotenv": {
"version": "8.2.0",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz",
"integrity": "sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw=="
},
"duplexer3": {
"version": "0.1.4",
"resolved": "https://registry.npmjs.org/duplexer3/-/duplexer3-0.1.4.tgz",
......@@ -1669,6 +1688,33 @@
}
}
},
"express-session": {
"version": "1.17.1",
"resolved": "https://registry.npmjs.org/express-session/-/express-session-1.17.1.tgz",
"integrity": "sha512-UbHwgqjxQZJiWRTMyhvWGvjBQduGCSBDhhZXYenziMFjxst5rMV+aJZ6hKPHZnPyHGsrqRICxtX8jtEbm/z36Q==",
"requires": {
"cookie": "0.4.0",
"cookie-signature": "1.0.6",
"debug": "2.6.9",
"depd": "~2.0.0",
"on-headers": "~1.0.2",
"parseurl": "~1.3.3",
"safe-buffer": "5.2.0",
"uid-safe": "~2.1.5"
},
"dependencies": {
"depd": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz",
"integrity": "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw=="
},
"safe-buffer": {
"version": "5.2.0",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.0.tgz",
"integrity": "sha512-fZEwUGbVl7kouZs1jCdMLdt95hdIv0ZeHg6L7qPeciMZhZ+/gdesW4wgTARkrFWEpspjEATAzUGPG8N2jJiwbg=="
}
}
},
"ext": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/ext/-/ext-1.4.0.tgz",
......@@ -3945,6 +3991,11 @@
"ee-first": "1.1.1"
}
},
"on-headers": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.2.tgz",
"integrity": "sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA=="
},
"once": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
......@@ -4125,6 +4176,28 @@
"resolved": "https://registry.npmjs.org/pascalcase/-/pascalcase-0.1.1.tgz",
"integrity": "sha1-s2PlXoAGym/iF4TS2yK9FdeRfxQ="
},
"passport": {
"version": "0.4.1",
"resolved": "https://registry.npmjs.org/passport/-/passport-0.4.1.tgz",
"integrity": "sha512-IxXgZZs8d7uFSt3eqNjM9NQ3g3uQCW5avD8mRNoXV99Yig50vjuaez6dQK2qC0kVWPRTujxY0dWgGfT09adjYg==",
"requires": {
"passport-strategy": "1.x.x",
"pause": "0.0.1"
}
},
"passport-local": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/passport-local/-/passport-local-1.0.0.tgz",
"integrity": "sha1-H+YyaMkudWBmJkN+O5BmYsFbpu4=",
"requires": {
"passport-strategy": "1.x.x"
}
},
"passport-strategy": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/passport-strategy/-/passport-strategy-1.0.0.tgz",
"integrity": "sha1-tVOaqPwiWj0a0XlHbd8ja0QPUuQ="
},
"path-dirname": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/path-dirname/-/path-dirname-1.0.2.tgz",
......@@ -4178,6 +4251,11 @@
"pinkie-promise": "^2.0.0"
}
},
"pause": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/pause/-/pause-0.0.1.tgz",
"integrity": "sha1-HUCLP9t2kjuVQ9lvtMnf1TXZy10="
},
"performance-now": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz",
......@@ -4313,6 +4391,11 @@
"resolved": "https://registry.npmjs.org/qs/-/qs-6.5.2.tgz",
"integrity": "sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA=="
},
"random-bytes": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/random-bytes/-/random-bytes-1.0.0.tgz",
"integrity": "sha1-T2ih3Arli9P7lYSMMDJNt11kNgs="
},
"randomatic": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/randomatic/-/randomatic-3.1.1.tgz",
......@@ -5370,6 +5453,14 @@
"is-typedarray": "^1.0.0"
}
},
"uid-safe": {
"version": "2.1.5",
"resolved": "https://registry.npmjs.org/uid-safe/-/uid-safe-2.1.5.tgz",
"integrity": "sha512-KPHm4VL5dDXKz01UuEd88Df+KzynaohSL9fBh096KWAxSKZQDI2uBrVqtvRM4rwrIrRRKsdLNML/lnaaVSRioA==",
"requires": {
"random-bytes": "~1.0.0"
}
},
"unc-path-regex": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/unc-path-regex/-/unc-path-regex-0.1.2.tgz",
......
......@@ -58,6 +58,10 @@ page('/signup', async function () {
await renderTemplate(templates('public/templates/signup.mustache'), context);
});
page('/login', async function () {
await renderTemplate(templates('public/templates/login.mustache'), context);
});
// On démarre le routing
page.start();
......
C'est l'heure de se connecter (☞゚ヮ゚)☞
<form method="post" action="/api/users/login">
<label for="username">Nom d'utilisateur</label>
<input type="text" id="username" name="username" required>
<label for="password">Mot de passe</label>
<input type="text" id="password" name="password" required>
<input type="submit" value="Se connecter">
</form>
\ No newline at end of file
// les require
const express = require('express');
require('dotenv').config();
const app = express();
const bodyParser = require('body-parser');
const userRouter = require('./api/users/user.router');
const auth = require('./auth/auth.js');
// on met en place une authentification valide pour toute le site
const passport = auth(app);
// Le contenu statique public sera lu à partir du repertoire 'public'
app.use('/public', express.static('public'));
// On sert le contenu du répertoire storage_H par l'url /storage
......@@ -18,11 +23,11 @@ app.use(bodyParser.urlencoded({
app.use(bodyParser.json());
// on redirige ce qui vient de /api/users vers userRouter
app.use('/api/users', userRouter);
app.use('/api/users', userRouter(passport));
// Pour toutes les autres url (catch all) on renverra l'index.html
// c'est le routeur coté client qui fera alors le routing
app.use(function (req, res) {
res.sendFile('public/index.html', {'root': __dirname});
});
app.listen(8080, () => { console.log('Server is running on port 8080')});
\ No newline at end of file
app.listen(process.env.APP_PORT, () => { console.log('Server is running on port ' + process.env.APP_PORT )});
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment