chore: fix lint errors

This commit is contained in:
Paul Bienkowski 2020-11-20 11:06:32 +01:00
parent 481999b278
commit b6ab3d0b1d
5 changed files with 20 additions and 14 deletions

View file

@ -2,17 +2,21 @@ module.exports = errorHandler;
function errorHandler(err, req, res, next) {
switch (true) {
case typeof err === 'string':
case typeof err === 'string': {
// custom application error
const is404 = err.toLowerCase().endsWith('not found');
const statusCode = is404 ? 404 : 400;
return res.status(statusCode).json({ message: err });
}
case err.name === 'ValidationError':
// mongoose validation error
return res.status(400).json({ message: err.message });
case err.name === 'UnauthorizedError':
// jwt authentication error
return res.status(401).json({ message: 'Unauthorized' });
default:
return res.status(500).json({ message: err.message });
}

View file

@ -1,4 +1,4 @@
const crypto = require('crypto');
const crypto = require('crypto');
const mongoose = require('mongoose');
const sendEmail = require('../_helpers/send-email');
const User = mongoose.model('User');
@ -36,7 +36,9 @@ async function register(params, origin) {
async function verifyEmail({ token }) {
const account = await User.findOne({ verificationToken: token });
if (!account) throw 'Verification failed';
if (!account) {
throw Error('Verification failed');
}
account.needsEmailValidation = false;
account.verificationToken = undefined;
@ -70,7 +72,9 @@ async function validateResetToken({ token }) {
'resetToken.expires': { $gt: Date.now() },
});
if (!account) throw 'Invalid token';
if (!account) {
throw Error('Invalid token');
}
}
async function resetPassword({ token, password }) {
@ -79,7 +83,9 @@ async function resetPassword({ token, password }) {
'resetToken.expires': { $gt: Date.now() },
});
if (!account) throw 'Invalid token';
if (!account) {
throw Error('Invalid token');
}
// update password and remove reset token
account.setPassword(password);

10
app.js
View file

@ -1,11 +1,8 @@
const http = require('http');
const path = require('path');
const methods = require('methods');
const express = require('express');
const bodyParser = require('body-parser');
const session = require('express-session');
const cors = require('cors');
const passport = require('passport');
const errorhandler = require('errorhandler');
const mongoose = require('mongoose');
@ -22,7 +19,7 @@ app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: false }));
app.use(require('method-override')());
app.use(express.static(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({ secret: 'obsobs', cookie: { maxAge: 60000 }, resave: false, saveUninitialized: false }));
@ -82,6 +79,7 @@ app.use(function (err, req, res, next) {
});
// finally, let's start our server...
var server = app.listen(process.env.PORT || 3000, function () {
console.log('Listening on port ' + server.address().port);
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log('Listening on port ' + port);
});

View file

@ -1,8 +1,6 @@
const mongoose = require('mongoose');
const uniqueValidator = require('mongoose-unique-validator');
const slug = require('slug');
const User = mongoose.model('User');
const TrackData = mongoose.model('TrackData');
const TrackSchema = new mongoose.Schema(
{

View file

@ -39,7 +39,7 @@ router.put('/user', auth.required, function (req, res, next) {
if (typeof req.body.user.areTracksVisibleForAll !== 'undefined') {
user.areTracksVisibleForAll = req.body.user.areTracksVisibleForAll;
}
if (typeof req.body.user.password !== 'undefined' && req.body.user.password != '') {
if (typeof req.body.user.password === 'string' && req.body.user.password !== '') {
user.setPassword(req.body.user.password);
}