📦 NEW: GET /users

This commit is contained in:
Divlo 2020-05-02 15:51:51 +02:00
parent 650bcac05a
commit db3f940da5
3 changed files with 94 additions and 66 deletions

View File

@ -9,7 +9,7 @@ const Sequelize = require('sequelize');
exports.getFunctions = async (req, res, next) => { exports.getFunctions = async (req, res, next) => {
const categoryId = helperQueryNumber(req.query.categoryId, 0); const categoryId = helperQueryNumber(req.query.categoryId, 0);
let search = req.query.search; let { search } = req.query;
try { search = search.toLowerCase(); } catch {}; try { search = search.toLowerCase(); } catch {};
const options = { const options = {
where: { where: {

View File

@ -4,6 +4,7 @@ const bcrypt = require('bc
const jwt = require('jsonwebtoken'); const jwt = require('jsonwebtoken');
const ms = require('ms'); const ms = require('ms');
const uuid = require('uuid'); const uuid = require('uuid');
const Sequelize = require('sequelize');
const errorHandling = require('../assets/utils/errorHandling'); const errorHandling = require('../assets/utils/errorHandling');
const { serverError, generalError } = require('../assets/config/errors'); const { serverError, generalError } = require('../assets/config/errors');
const { JWT_SECRET, FRONT_END_HOST, EMAIL_INFO, HOST, TOKEN_LIFE } = require('../assets/config/config'); const { JWT_SECRET, FRONT_END_HOST, EMAIL_INFO, HOST, TOKEN_LIFE } = require('../assets/config/config');
@ -16,6 +17,7 @@ const Categories = require('..
const Comments = require('../models/comments'); const Comments = require('../models/comments');
const Quotes = require('../models/quotes'); const Quotes = require('../models/quotes');
const deleteFilesNameStartWith = require('../assets/utils/deleteFilesNameStartWith'); const deleteFilesNameStartWith = require('../assets/utils/deleteFilesNameStartWith');
const getPagesHelper = require('../assets/utils/getPagesHelper');
async function handleEditUser(res, { name, email, biography, isPublicEmail }, userId, logoName) { async function handleEditUser(res, { name, email, biography, isPublicEmail }, userId, logoName) {
const user = await Users.findOne({ where: { id: userId } }); const user = await Users.findOne({ where: { id: userId } });
@ -43,6 +45,25 @@ async function handleEditUser(res, { name, email, biography, isPublicEmail }, us
return res.status(200).json({ id: user.id, name: user.name, email: user.email, biography: user.biography, logo: user.logo, isPublicEmail: user.isPublicEmail, isAdmin: user.isAdmin, createdAt: user.createdAt }); return res.status(200).json({ id: user.id, name: user.name, email: user.email, biography: user.biography, logo: user.logo, isPublicEmail: user.isPublicEmail, isAdmin: user.isAdmin, createdAt: user.createdAt });
} }
exports.getUsers = async (req, res, next) => {
let { search } = req.query;
try { search = search.toLowerCase(); } catch {};
const options = {
where: {
isConfirmed: true,
// Recherche
...(search != undefined) && {
name: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('name')), 'LIKE', `%${search}%`)
}
},
attributes: {
exclude: ["updatedAt", "isAdmin", "isConfirmed", "password", "tempToken", "tempExpirationToken", "isPublicEmail", "email"]
},
order: [['createdAt', 'DESC']]
};
return await getPagesHelper({ req, res, next }, Users, options);
}
exports.putUser = async (req, res, next) => { exports.putUser = async (req, res, next) => {
const { name, email, biography, isPublicEmail } = req.body; const { name, email, biography, isPublicEmail } = req.body;
const logo = req.files.logo; const logo = req.files.logo;

View File

@ -8,23 +8,13 @@ const isAuth = require('../middlewares/isAuth');
const UsersRouter = Router(); const UsersRouter = Router();
// Permet de se connecter UsersRouter.route('/')
UsersRouter.post('/login', [
body('email')
.not()
.isEmpty()
.withMessage(requiredFields.message),
body('password')
.not()
.isEmpty()
.withMessage(requiredFields.message)
], usersController.login);
// Récupère les informations public d'un profil // Récupère les utilisateurs
UsersRouter.get('/:name', usersController.getUserInfo); .get(usersController.getUsers)
// Permet de modifier son profil // Permet de modifier son profil
UsersRouter.put('/', isAuth, .put(isAuth,
fileUpload({ fileUpload({
useTempFiles: true, useTempFiles: true,
safeFileNames: true, safeFileNames: true,
@ -76,6 +66,21 @@ fileUpload({
.escape() .escape()
], usersController.putUser); ], usersController.putUser);
// Permet de se connecter
UsersRouter.post('/login', [
body('email')
.not()
.isEmpty()
.withMessage(requiredFields.message),
body('password')
.not()
.isEmpty()
.withMessage(requiredFields.message)
], usersController.login);
// Récupère les informations public d'un profil
UsersRouter.get('/:name', usersController.getUserInfo);
// Permet de s'inscrire // Permet de s'inscrire
UsersRouter.post('/register', [ UsersRouter.post('/register', [
body('email') body('email')
@ -121,15 +126,17 @@ UsersRouter.post('/register', [
// Confirme l'inscription // Confirme l'inscription
UsersRouter.get('/confirm-email/:tempToken', usersController.confirmEmail); UsersRouter.get('/confirm-email/:tempToken', usersController.confirmEmail);
UsersRouter.route('/reset-password')
// Demande une réinitialisation du mot de passe // Demande une réinitialisation du mot de passe
UsersRouter.post('/reset-password', [ .post([
body('email') body('email')
.isEmail() .isEmail()
.withMessage("Veuillez rentré une adresse mail valide.") .withMessage("Veuillez rentré une adresse mail valide.")
], usersController.resetPassword); ], usersController.resetPassword)
// Nouveau mot de passe // Nouveau mot de passe
UsersRouter.put('/reset-password', [ .put([
body('password') body('password')
.isLength({ min: 4 }) .isLength({ min: 4 })
.withMessage("Votre mot de passe est trop court!") .withMessage("Votre mot de passe est trop court!")