mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
add res.respond middleware... and use it in user controller
This commit is contained in:
@@ -95,7 +95,11 @@ api.registerLocal = {
|
||||
}
|
||||
})
|
||||
.then((savedUser) => {
|
||||
res.status(201).json(savedUser);
|
||||
if (savedUser.auth.facebook.id) {
|
||||
res.respond(200, savedUser.auth.local); // TODO make sure this used .toJSON and removes private fields
|
||||
} else {
|
||||
res.respond(201, savedUser);
|
||||
}
|
||||
|
||||
// Clean previous email preferences
|
||||
EmailUnsubscription
|
||||
@@ -117,7 +121,7 @@ api.registerLocal = {
|
||||
|
||||
function _loginRes (user, req, res, next) {
|
||||
if (user.auth.blocked) return next(new NotAuthorized(res.t('accountSuspended', {userId: user._id})));
|
||||
res.status(200).json({id: user._id, apiToken: user.apiToken});
|
||||
res.respond(200, {id: user._id, apiToken: user.apiToken});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -254,7 +258,7 @@ api.deleteSocial = {
|
||||
if (!user.auth.local.username) return next(new NotAuthorized(res.t('cantDetachFb'))); // TODO move to model validation?
|
||||
|
||||
User.update({_id: user._id}, {$unset: {'auth.facebook': 1}})
|
||||
.then(() => res.status(200).json({ok: true})) // TODO standardize this type of response
|
||||
.then(() => res.respond(200))
|
||||
.catch(next);
|
||||
},
|
||||
};
|
||||
|
||||
9
website/src/middlewares/api-v3/response.js
Normal file
9
website/src/middlewares/api-v3/response.js
Normal file
@@ -0,0 +1,9 @@
|
||||
export default function responseHandler (req, res, next) {
|
||||
res.respond = function respond (status = 200, data = {}) {
|
||||
res.status(status);
|
||||
data.success = status >= 400 ? false : true; // TODO the data object should be cloned to avoid pollution?
|
||||
res.json(data);
|
||||
};
|
||||
|
||||
next();
|
||||
}
|
||||
Reference in New Issue
Block a user