v3: misc fixes
This commit is contained in:
@@ -156,12 +156,14 @@ api.registerLocal = {
|
||||
uuid: savedUser._id,
|
||||
});
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
};
|
||||
|
||||
function _loginRes (user, req, res) {
|
||||
if (user.auth.blocked) throw new NotAuthorized(res.t('accountSuspended', {userId: user._id}));
|
||||
res.respond(200, {id: user._id, apiToken: user.apiToken});
|
||||
return res.respond(200, {id: user._id, apiToken: user.apiToken});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -210,7 +212,7 @@ api.loginLocal = {
|
||||
let user = await User.findOne(login, {auth: 1, apiToken: 1}).exec();
|
||||
let isValidPassword = user && user.auth.local.hashed_password === passwordUtils.encrypt(req.body.password, user.auth.local.salt);
|
||||
if (!isValidPassword) throw new NotAuthorized(res.t('invalidLoginCredentialsLong'));
|
||||
_loginRes(user, ...arguments);
|
||||
return _loginRes(user, ...arguments);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -275,6 +277,8 @@ api.loginSocial = {
|
||||
gaLabel: network,
|
||||
uuid: savedUser._id,
|
||||
});
|
||||
|
||||
return null;
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
@@ -108,6 +108,8 @@ api.createChallengeTasks = {
|
||||
|
||||
// If adding tasks to a challenge -> sync users
|
||||
if (challenge) challenge.addTasks(tasks);
|
||||
|
||||
return null;
|
||||
},
|
||||
};
|
||||
|
||||
@@ -185,7 +187,7 @@ api.getUserTasks = {
|
||||
let validationErrors = req.validationErrors();
|
||||
if (validationErrors) throw validationErrors;
|
||||
|
||||
await _getTasks(req, res, res.locals.user);
|
||||
return await _getTasks(req, res, res.locals.user);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -220,7 +222,7 @@ api.getChallengeTasks = {
|
||||
let group = await Group.getGroup({user, groupId: challenge.group, fields: '_id type privacy', optionalMembership: true});
|
||||
if (!group || !challenge.canView(user, group)) throw new NotFound(res.t('challengeNotFound'));
|
||||
|
||||
await _getTasks(req, res, res.locals.user, challenge);
|
||||
return await _getTasks(req, res, res.locals.user, challenge);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -312,6 +314,8 @@ api.updateTask = {
|
||||
let savedTask = await task.save();
|
||||
res.respond(200, savedTask);
|
||||
if (challenge) challenge.updateTask(savedTask);
|
||||
|
||||
return null;
|
||||
},
|
||||
};
|
||||
|
||||
@@ -418,6 +422,8 @@ api.scoreTask = {
|
||||
logger.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
};
|
||||
|
||||
@@ -522,6 +528,8 @@ api.addChecklistItem = {
|
||||
|
||||
res.respond(200, savedTask);
|
||||
if (challenge) challenge.updateTask(savedTask);
|
||||
|
||||
return null;
|
||||
},
|
||||
};
|
||||
|
||||
@@ -615,6 +623,8 @@ api.updateChecklistItem = {
|
||||
|
||||
res.respond(200, savedTask);
|
||||
if (challenge) challenge.updateTask(savedTask);
|
||||
|
||||
return null;
|
||||
},
|
||||
};
|
||||
|
||||
@@ -664,6 +674,8 @@ api.removeChecklistItem = {
|
||||
let savedTask = await task.save();
|
||||
res.respond(200, savedTask);
|
||||
if (challenge) challenge.updateTask(savedTask);
|
||||
|
||||
return null;
|
||||
},
|
||||
};
|
||||
|
||||
@@ -877,6 +889,8 @@ api.deleteTask = {
|
||||
|
||||
res.respond(200, {});
|
||||
if (challenge) challenge.removeTask(task);
|
||||
|
||||
return null;
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -80,6 +80,7 @@ let updatablePaths = [
|
||||
'flags.welcomed',
|
||||
'flags.cardReceived',
|
||||
'flags.warnedLowHealth',
|
||||
'flags.newStuff',
|
||||
|
||||
'achievements',
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ api.getFrontPage = {
|
||||
return res.redirect('/static/front');
|
||||
}
|
||||
|
||||
res.render('index.jade', {
|
||||
return res.render('index.jade', {
|
||||
title: 'Habitica | Your Life The Role Playing Game',
|
||||
env: res.locals.habitrpg,
|
||||
});
|
||||
@@ -40,7 +40,7 @@ _.each(staticPages, (name) => {
|
||||
middlewares: [locals],
|
||||
runCron: false,
|
||||
async handler (req, res) {
|
||||
res.render(`static/${name}.jade`, {
|
||||
return res.render(`static/${name}.jade`, {
|
||||
env: res.locals.habitrpg,
|
||||
md,
|
||||
userCount: TOTAL_USER_COUNT,
|
||||
@@ -58,7 +58,7 @@ _.each(shareables, (name) => {
|
||||
middlewares: [locals],
|
||||
runCron: false,
|
||||
async handler (req, res) {
|
||||
res.render(`social/${name}`, {
|
||||
return res.render(`social/${name}`, {
|
||||
env: res.locals.habitrpg,
|
||||
md,
|
||||
userCount: TOTAL_USER_COUNT,
|
||||
@@ -72,7 +72,7 @@ api.redirectExtensionsPage = {
|
||||
url: '/static/extensions',
|
||||
runCron: false,
|
||||
async handler (req, res) {
|
||||
res.redirect('http://habitica.wikia.com/wiki/App_and_Extension_Integrations');
|
||||
return res.redirect('http://habitica.wikia.com/wiki/App_and_Extension_Integrations');
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ const logger = new winston.Logger();
|
||||
if (IS_PROD) {
|
||||
// TODO production logging, use loggly and new relic too
|
||||
|
||||
if (ENABLE_CONSOLE_LOGS_IN_PROD) {
|
||||
if (ENABLE_CONSOLE_LOGS_IN_PROD === 'true') {
|
||||
logger.add(winston.transports.Console, {
|
||||
colorize: true,
|
||||
prettyPrint: true,
|
||||
|
||||
@@ -19,7 +19,7 @@ export function authWithHeaders (optional = false) {
|
||||
return next(new NotAuthorized(res.t('missingAuthHeaders')));
|
||||
}
|
||||
|
||||
User.findOne({
|
||||
return User.findOne({
|
||||
_id: userId,
|
||||
apiToken,
|
||||
})
|
||||
@@ -31,7 +31,7 @@ export function authWithHeaders (optional = false) {
|
||||
res.locals.user = user;
|
||||
// TODO use either session/cookie or headers, not both
|
||||
req.session.userId = user._id;
|
||||
next();
|
||||
return next();
|
||||
})
|
||||
.catch(next);
|
||||
};
|
||||
@@ -43,7 +43,7 @@ export function authWithSession (req, res, next) {
|
||||
|
||||
if (!userId) return next(new NotAuthorized(res.t('invalidCredentials')));
|
||||
|
||||
User.findOne({
|
||||
return User.findOne({
|
||||
_id: userId,
|
||||
})
|
||||
.exec()
|
||||
@@ -51,7 +51,7 @@ export function authWithSession (req, res, next) {
|
||||
if (!user) throw new NotAuthorized(res.t('invalidCredentials'));
|
||||
|
||||
res.locals.user = user;
|
||||
next();
|
||||
return next();
|
||||
})
|
||||
.catch(next);
|
||||
}
|
||||
@@ -64,12 +64,12 @@ export function authWithUrl (req, res, next) {
|
||||
throw new NotAuthorized(res.t('missingAuthParams'));
|
||||
}
|
||||
|
||||
User.findOne({ _id: userId, apiToken }).exec()
|
||||
return User.findOne({ _id: userId, apiToken }).exec()
|
||||
.then((user) => {
|
||||
if (!user) throw new NotAuthorized(res.t('invalidCredentials'));
|
||||
|
||||
res.locals.user = user;
|
||||
next();
|
||||
return next();
|
||||
})
|
||||
.catch(next);
|
||||
}
|
||||
|
||||
@@ -139,7 +139,7 @@ module.exports = function cronMiddleware (req, res, next) {
|
||||
toSave.push(task.save());
|
||||
});
|
||||
|
||||
Bluebird.all(toSave)
|
||||
return Bluebird.all(toSave)
|
||||
.then(saved => {
|
||||
user = res.locals.user = saved[0];
|
||||
if (!quest) return;
|
||||
@@ -150,6 +150,8 @@ module.exports = function cronMiddleware (req, res, next) {
|
||||
.then(() => User.findById(user._id).exec()) // fetch the updated user...
|
||||
.then(updatedUser => {
|
||||
res.locals.user = updatedUser;
|
||||
|
||||
return null;
|
||||
});
|
||||
})
|
||||
.then(() => next())
|
||||
|
||||
@@ -49,8 +49,8 @@ module.exports = function attachMiddlewares (app, server) {
|
||||
app.use(favicon(`${PUBLIC_DIR}/favicon.ico`));
|
||||
|
||||
app.use(cors);
|
||||
//app.use(forceSSL);
|
||||
//app.use(forceHabitica);
|
||||
app.use(forceSSL);
|
||||
app.use(forceHabitica);
|
||||
|
||||
app.use(bodyParser.urlencoded({
|
||||
extended: true, // Uses 'qs' library as old connect middleware
|
||||
@@ -70,9 +70,9 @@ module.exports = function attachMiddlewares (app, server) {
|
||||
app.use(passport.initialize());
|
||||
app.use(passport.session());
|
||||
|
||||
app.use(v3); // the main app, also setup top-level routes
|
||||
app.use('/api/v2', v2);
|
||||
app.use('/api/v1', v1);
|
||||
app.use(v3); // the main app, also setup top-level routes
|
||||
staticMiddleware(app);
|
||||
|
||||
app.use(notFoundHandler);
|
||||
|
||||
@@ -74,7 +74,7 @@ export function getUserLanguage (req, res, next) {
|
||||
req.language = _getFromUser(req.locals.user, req);
|
||||
return next();
|
||||
} else if (req.session && req.session.userId) { // Same thing if the user has a valid session
|
||||
User.findOne({
|
||||
return User.findOne({
|
||||
_id: req.session.userId,
|
||||
}, 'preferences.language')
|
||||
.lean()
|
||||
|
||||
Reference in New Issue
Block a user