Files
2025-08-08 10:47:29 +02:00

127 lines
4.6 KiB
JavaScript

var router = require('express').Router(),
mongoose = require('mongoose'),
User = mongoose.model('User');
const passport = require('passport'),
auth = require('../auth');
router.get('/user', auth.required, function (req, res, next) {
//console.log(req.payload);
User.findById(req.payload.id).then(function (user) {
if (!user) {
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
}
return res.json({ user: user.toAuthJSON() });
}).catch(next);
});
router.put('/user', auth.required, function (req, res, next) {
User.findById(req.payload.id).then(function (user) {
if (!user) {
return res.status(401).json({ errors: { "Unauthorized": "autentification requise" } });
}
if (typeof req.body.user.username !== 'undefined') {
user.username = req.body.user.username;
}
if (typeof req.body.user.email !== 'undefined') {
user.email = req.body.user.email;
}
if (typeof req.body.user.firstname !== 'undefined') {
user.firstname = req.body.user.firstname;
}
if (typeof req.body.user.lastname !== 'undefined') {
user.lastname = req.body.user.lastname;
}
if (typeof req.body.user.phone !== 'undefined') {
user.phone = req.body.user.phone;
}
if (typeof req.body.user.licence !== 'undefined') {
user.licence = req.body.user.licence;
}
if (typeof req.body.user.poids !== 'undefined') {
user.poids = req.body.user.poids;
}
if (typeof req.body.user.image !== 'undefined') {
user.image = req.body.user.image;
}
if (typeof req.body.user.bg_image !== 'undefined') {
user.bg_image = req.body.user.bg_image;
}
if (typeof req.body.user.password !== 'undefined') {
user.setPassword(req.body.user.password);
}
if (typeof req.body.user.role !== 'undefined') {
user.role = req.body.user.role;
}
return user.save().then(function () {
return res.json({ user: user.toAuthJSON() });
});
}).catch(next);
});
router.post('/users/login', function (req, res, next) {
console.log(req.body.user);
if (!req.body.user.email) {
return res.status(422).json({ errors: { email: "email can't be blank" } });
}
if (!req.body.user.password) {
return res.status(422).json({ errors: { password: " password can't be blank" } });
}
passport.authenticate('local', { session: false }, function (err, user, info) {
if (err) {
return next(err);
}
if (user) {
user.token = user.generateJWT();
return res.json({ user: user.toAuthJSON() });
} else {
return res.status(422).json(info);
}
})(req, res, next);
});
router.get('/authenticate', function (req, res, next) {
passport.authenticate('headerapikey', { session: false }, function (err, application, info) {
if (err) {
return res.status(err.status || 500).json({message: "An authentication error occured.", err: err.message});
//return next(err);
}
if (!application) {
return res.status(401).json({message: "Unauthorized - You are not allowed to access this resource.", err: err, info: info});
}
if (application.author) {
application.author.token = application.author.generateJWT();
return res.json({ user: application.author.toAuthJSON(), application: application.toAuthJSON() });
} else {
return res.status(422).json(info);
}
})(req, res, next);
});
router.post('/users', function (req, res, next) {
console.log('ici post users');
let user = new User();
if (typeof req.body.user.username !== 'undefined') {
user.username = req.body.user.username;
} else {
user.username = `${req.body.user.firstname}_${req.body.user.lastname}`;
}
user.email = req.body.user.email;
user.firstname = req.body.user.firstname;
user.lastname = req.body.user.lastname;
user.phone = req.body.user.phone;
if (typeof req.body.user.licence !== 'undefined') {
user.licence = req.body.user.licence;
}
if (typeof req.body.user.poids !== 'undefined') {
user.poids = req.body.user.poids;
}
user.setPassword(req.body.user.password);
user.role = 'User';
user.save().then(function () {
return res.json({ user: user.toAuthJSON() });
}).catch(next);
});
module.exports = router;