2020-08-03 12:04:07 +02:00
|
|
|
const { Router } = require('express')
|
|
|
|
const { body } = require('express-validator')
|
|
|
|
const fileUpload = require('express-fileupload')
|
|
|
|
const usersController = require('../controllers/users')
|
|
|
|
const { requiredFields } = require('../assets/config/errors')
|
|
|
|
const Users = require('../models/users')
|
|
|
|
const isAuth = require('../middlewares/isAuth')
|
2020-03-25 16:23:43 +01:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
const UsersRouter = Router()
|
2020-03-25 16:23:43 +01:00
|
|
|
|
2020-05-02 15:51:51 +02:00
|
|
|
UsersRouter.route('/')
|
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
// Récupère les utilisateurs
|
|
|
|
.get(usersController.getUsers)
|
2020-05-02 15:51:51 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
// Permet de modifier son profil
|
|
|
|
.put(isAuth,
|
|
|
|
fileUpload({
|
|
|
|
useTempFiles: true,
|
|
|
|
safeFileNames: true,
|
|
|
|
preserveExtension: Number,
|
|
|
|
limits: { fileSize: 5 * 1024 * 1024 }, // 5mb,
|
|
|
|
parseNested: true
|
2020-05-02 15:51:51 +02:00
|
|
|
}),
|
|
|
|
[
|
2020-08-03 12:04:07 +02:00
|
|
|
body('email')
|
2020-03-25 16:23:43 +01:00
|
|
|
.isEmail()
|
2020-08-03 12:04:07 +02:00
|
|
|
.withMessage('Veuillez rentré une adresse mail valide.')
|
|
|
|
.custom(async (email) => {
|
|
|
|
try {
|
|
|
|
const user = await Users.findOne({ where: { email } })
|
|
|
|
if (user && user.email !== email) {
|
|
|
|
return Promise.reject(new Error("L'adresse email existe déjà..."))
|
2020-03-25 16:23:43 +01:00
|
|
|
}
|
2020-08-03 12:04:07 +02:00
|
|
|
} catch (error) {
|
|
|
|
return console.log(error)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2020-03-25 16:23:43 +01:00
|
|
|
.normalizeEmail(),
|
2020-08-03 12:04:07 +02:00
|
|
|
body('name')
|
2020-03-25 16:23:43 +01:00
|
|
|
.trim()
|
|
|
|
.not()
|
|
|
|
.isEmpty()
|
2020-08-03 12:04:07 +02:00
|
|
|
.withMessage('Vous devez avoir un nom (ou pseudo).')
|
2020-03-25 16:23:43 +01:00
|
|
|
.isAlphanumeric()
|
2020-08-03 12:04:07 +02:00
|
|
|
.withMessage('Votre nom ne peut contenir que des lettres ou/et des nombres.')
|
2020-03-25 22:59:08 +01:00
|
|
|
.isLength({ max: 30 })
|
2020-08-03 12:04:07 +02:00
|
|
|
.withMessage('Votre nom est trop long')
|
2020-03-25 22:59:08 +01:00
|
|
|
.custom(async (name) => {
|
2020-08-03 12:04:07 +02:00
|
|
|
try {
|
|
|
|
const user = await Users.findOne({ where: { name } })
|
|
|
|
if (user && user.name !== name) {
|
|
|
|
return Promise.reject(new Error('Le nom existe déjà...'))
|
2020-03-25 16:23:43 +01:00
|
|
|
}
|
2020-08-03 12:04:07 +02:00
|
|
|
} catch (error) {
|
|
|
|
console.log(error)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}),
|
|
|
|
body('isPublicEmail')
|
|
|
|
.isBoolean()
|
|
|
|
.withMessage("L'adresse email peut être public ou privé, rien d'autre."),
|
|
|
|
body('biography')
|
|
|
|
.trim()
|
|
|
|
.escape()
|
|
|
|
], 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
|
|
|
|
UsersRouter.post('/register', [
|
|
|
|
body('email')
|
|
|
|
.isEmail()
|
|
|
|
.withMessage('Veuillez rentré une adresse mail valide.')
|
|
|
|
.custom(async (email) => {
|
|
|
|
try {
|
|
|
|
const user = await Users.findOne({ where: { email } })
|
|
|
|
if (user) {
|
|
|
|
return Promise.reject(new Error("L'adresse email existe déjà..."))
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
return console.log(error)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
.normalizeEmail(),
|
|
|
|
body('password')
|
|
|
|
.isLength({ min: 4 })
|
|
|
|
.withMessage('Votre mot de passe est trop court!'),
|
|
|
|
body('name')
|
|
|
|
.trim()
|
|
|
|
.not()
|
|
|
|
.isEmpty()
|
|
|
|
.withMessage('Vous devez avoir un nom (ou pseudo).')
|
|
|
|
.isAlphanumeric()
|
|
|
|
.withMessage('Votre nom ne peut contenir que des lettres ou/et des nombres.')
|
|
|
|
.isLength({ max: 30 })
|
|
|
|
.withMessage('Votre nom est trop long')
|
|
|
|
.custom(async (name) => {
|
|
|
|
try {
|
|
|
|
const user = await Users.findOne({ where: { name } })
|
|
|
|
if (user) {
|
|
|
|
return Promise.reject(new Error('Le nom existe déjà...'))
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
], usersController.register)
|
2020-03-25 16:23:43 +01:00
|
|
|
|
|
|
|
// Confirme l'inscription
|
2020-08-03 12:04:07 +02:00
|
|
|
UsersRouter.get('/confirm-email/:tempToken', usersController.confirmEmail)
|
2020-03-25 16:23:43 +01:00
|
|
|
|
2020-05-02 15:51:51 +02:00
|
|
|
UsersRouter.route('/reset-password')
|
2020-03-31 07:48:00 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
// Demande une réinitialisation du mot de passe
|
|
|
|
.post([
|
|
|
|
body('email')
|
|
|
|
.isEmail()
|
|
|
|
.withMessage('Veuillez rentré une adresse mail valide.')
|
|
|
|
], usersController.resetPassword)
|
2020-05-02 15:51:51 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
// Nouveau mot de passe
|
|
|
|
.put([
|
|
|
|
body('password')
|
|
|
|
.isLength({ min: 4 })
|
|
|
|
.withMessage('Votre mot de passe est trop court!')
|
|
|
|
], usersController.newPassword)
|
2020-03-31 07:48:00 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
module.exports = UsersRouter
|