---|main| Initial commit

This commit is contained in:
Julien Gautier
2023-09-12 21:43:57 +02:00
commit 7e9599118e
42 changed files with 8015 additions and 0 deletions
+117
View File
@@ -0,0 +1,117 @@
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) {
User.findById(req.payload.id).then(function (user) {
if (!user) {
return res.sendStatus(401);
}
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.sendStatus(401);
}
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.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) {
if (!req.body.user.email) {
return res.status(422).json({ errors: { email: "can't be blank" } });
}
if (!req.body.user.password) {
return res.status(422).json({ errors: { 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) {
var 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;
}
user.setPassword(req.body.user.password);
user.role = 'User'; // valeurs possibles : 'User' or 'Admin'
user.save().then(function () {
return res.json({ user: user.toAuthJSON() });
}).catch(next);
});
module.exports = router;