Fix Eslint errors

develop
sundowndev 2018-11-16 12:28:37 +01:00
parent 901b6b0db8
commit 9d8c517b82
14 changed files with 88 additions and 22 deletions

View File

@ -1,3 +1,6 @@
{
"extends": "airbnb-base"
}
"extends": "airbnb-base",
"rules": {
"max-len": ["error", { "comments": 120 }]
}
}

View File

@ -10,7 +10,11 @@ const UserSchema = new Schema({
type: String, required: [true, "can't be blank"],
},
email: {
type: String, lowercase: true, unique: true, required: [true, "can't be blank"], index: true,
type: String,
lowercase: true,
unique: true,
required: [true, "can't be blank"],
index: true,
},
password: {
type: String, required: [true, "can't be blank"],

View File

@ -13,7 +13,10 @@ module.exports = (req, res, next) => {
if (err) throw err;
if (!user) {
return next({ status: 400, message: 'Authentication failed. User not found.' });
return next({
status: 400,
message: 'Authentication failed. User not found.',
});
}
// check if password matches
@ -25,7 +28,11 @@ module.exports = (req, res, next) => {
email: user.email,
};
const token = jwt.sign({ user: dataUser }, secret, { expiresIn: '12h' });
const token = jwt.sign({
user: dataUser,
}, secret, {
expiresIn: '12h',
});
// return the information including token as JSON
return res.json({
@ -40,7 +47,10 @@ module.exports = (req, res, next) => {
});
}
return next({ status: 401, message: 'Authentication failed. Wrong password.' });
return next({
status: 401,
message: 'Authentication failed. Wrong password.',
});
});
});
};

View File

@ -15,12 +15,19 @@ module.exports = (req, res, next) => {
UserModel.countDocuments({ email: req.body.email }, (err, c) => {
if (c !== 0) {
return next({ status: 401, message: 'Email is already taken by another user.' });
return next({
status: 401,
message: 'Email is already taken by another user.',
});
}
return User.save((saveErr) => {
if (saveErr) {
return next({ status: 500, message: 'Database error', error: [saveErr] });
return next({
status: 500,
message: 'Database error',
error: [saveErr],
});
}
return res.status(201).json({ success: true, message: 'Success' });

View File

@ -6,7 +6,11 @@ module.exports = (req, res, next) => {
const { user } = jwt.decode(req.headers.authorization);
const Note = new NoteModel({ title: req.body.title, text: req.body.text, user: user.id });
const Note = new NoteModel({
title: req.body.title,
text: req.body.text,
user: user.id,
});
Note.save((err) => {
if (err) {

View File

@ -58,7 +58,7 @@ note.put('/:id', Authentication, UpdateValidation, update);
* "message": "Note successfully deleted."
* }
* @apiErrorExample {json} Error-Response:
* HTTP/1.1 403 Not Found
* HTTP/1.1 403 Forbidden
* {
* "success": false,
* "message": "Access forbidden.",

View File

@ -22,10 +22,13 @@ user.get('/me', Authentication, profile);
* @api {put} /user/me Update account information
* @apiName UpdateUser
* @apiGroup User
* @apiDescription Send only password and new_password to change the password. Otherwise they will be ignored.
*
* @apiParam {String} Firstname new firstname.
* @apiParam {String} Lastname new lastname.
* @apiParam {String} Email new email address.
* @apiParam {String} Firstname New firstname.
* @apiParam {String} Lastname New lastname.
* @apiParam {String} Email New email address.
* @apiParam {String} Password Actual password.
* @apiParam {String} Password New password.
*
* @apiSuccess {Object} user User object.
*/

View File

@ -1,5 +1,20 @@
module.exports = (req, res) => {
const user = {};
const mongoose = require('mongoose');
const jwt = require('jsonwebtoken');
res.status(200).json({ user });
module.exports = (req, res, next) => {
const UserModel = mongoose.model('User');
const { user } = jwt.decode(req.headers.authorization);
return UserModel.findById(user.id, 'id firstname lastname email')
.lean()
.exec()
.then((result) => {
if (result === null) {
return next({ status: 401, message: 'User does not exists.' });
}
return res.status(200).json(result);
})
.catch(() => next({ status: 401, message: 'User does not exists.' }));
};

View File

@ -16,7 +16,7 @@ module.exports = (req, res, next) => {
return next({ status: 401, error: validateErr.details });
}
return jwt.verify(req.headers.authorization, secret, (err, decoded) => {
return jwt.verify(req.headers.authorization, secret, (err) => {
if (err) {
return next({ status: 401, message: 'Token error.', error: [err] });
}

View File

@ -12,7 +12,11 @@ module.exports = (req, res, next) => {
},
schema, (validateErr) => {
if (validateErr) {
return next({ status: 400, message: 'Form is invalid.', error: validateErr.details });
return next({
status: 400,
message: 'Form is invalid.',
error: validateErr.details,
});
}
return next();

View File

@ -12,7 +12,11 @@ module.exports = (req, res, next) => {
},
schema, (validateErr) => {
if (validateErr) {
return next({ status: 400, message: 'Form is invalid.', error: validateErr.details });
return next({
status: 400,
message: 'Form is invalid.',
error: validateErr.details,
});
}
return next();

View File

@ -12,7 +12,11 @@ module.exports = (req, res, next) => {
},
schema, (validateErr) => {
if (validateErr) {
return next({ status: 400, message: 'Form is invalid.', error: validateErr.details });
return next({
status: 400,
message: 'Form is invalid.',
error: validateErr.details,
});
}
return next();

View File

@ -16,7 +16,11 @@ module.exports = (req, res, next) => {
},
schema, (validateErr) => {
if (validateErr) {
return next({ status: 400, message: 'Form is invalid.', error: validateErr.details });
return next({
status: 400,
message: 'Form is invalid.',
error: validateErr.details,
});
}
return next();

View File

@ -10,7 +10,11 @@ const port = process.env.PORT || 8080; // set our port
app.use('/', routes);
app.use((err, req, res, next) => {
res.status(err.status || 400).json({ success: false, message: err.message || 'An error occured.', errors: err.error || [] });
res.status(err.status || 400).json({
success: false,
message: err.message || 'An error occured.',
errors: err.error || [],
});
});
app.use((req, res) => {