Consistent user data returned from API
There's duplication in the user and session endpoints that all return the same shaped user model data. The new helper should keep them consistent when new properties need to be exposed.
This commit is contained in:
parent
693542eef5
commit
7c4f180540
2 changed files with 19 additions and 43 deletions
|
@ -1,5 +1,7 @@
|
|||
import passport from 'passport';
|
||||
|
||||
import { userResponse } from './user.controller';
|
||||
|
||||
export function createSession(req, res, next) {
|
||||
passport.authenticate('local', (err, user) => { // eslint-disable-line consistent-return
|
||||
if (err) { return next(err); }
|
||||
|
@ -9,28 +11,14 @@ export function createSession(req, res, next) {
|
|||
|
||||
req.logIn(user, (innerErr) => {
|
||||
if (innerErr) { return next(innerErr); }
|
||||
return res.json({
|
||||
email: req.user.email,
|
||||
username: req.user.username,
|
||||
preferences: req.user.preferences,
|
||||
apiKeys: req.user.apiKeys,
|
||||
verified: req.user.verified,
|
||||
id: req.user._id
|
||||
});
|
||||
return res.json(userResponse(req.user));
|
||||
});
|
||||
})(req, res, next);
|
||||
}
|
||||
|
||||
export function getSession(req, res) {
|
||||
if (req.user) {
|
||||
return res.json({
|
||||
email: req.user.email,
|
||||
username: req.user.username,
|
||||
preferences: req.user.preferences,
|
||||
apiKeys: req.user.apiKeys,
|
||||
verified: req.user.verified,
|
||||
id: req.user._id
|
||||
});
|
||||
return res.json(userResponse(req.user));
|
||||
}
|
||||
return res.status(404).send({ message: 'Session does not exist' });
|
||||
}
|
||||
|
|
|
@ -10,6 +10,17 @@ import {
|
|||
|
||||
export * from './user.controller/apiKey';
|
||||
|
||||
export function userResponse(user) {
|
||||
return {
|
||||
email: user.email,
|
||||
username: user.username,
|
||||
preferences: user.preferences,
|
||||
apiKeys: user.apiKeys,
|
||||
verified: user.verified,
|
||||
id: user._id
|
||||
};
|
||||
}
|
||||
|
||||
const random = (done) => {
|
||||
crypto.randomBytes(20, (err, buf) => {
|
||||
const token = buf.toString('hex');
|
||||
|
@ -78,13 +89,7 @@ export function createUser(req, res, next) {
|
|||
});
|
||||
|
||||
mail.send(mailOptions, (mailErr, result) => { // eslint-disable-line no-unused-vars
|
||||
res.json({
|
||||
email: req.user.email,
|
||||
username: req.user.username,
|
||||
preferences: req.user.preferences,
|
||||
verified: req.user.verified,
|
||||
id: req.user._id
|
||||
});
|
||||
res.json(userResponse(req.user));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -224,13 +229,7 @@ export function emailVerificationInitiate(req, res) {
|
|||
user.verifiedTokenExpires = EMAIL_VERIFY_TOKEN_EXPIRY_TIME; // 24 hours
|
||||
user.save();
|
||||
|
||||
res.json({
|
||||
email: req.user.email,
|
||||
username: req.user.username,
|
||||
preferences: req.user.preferences,
|
||||
verified: user.verified,
|
||||
id: req.user._id
|
||||
});
|
||||
res.json(userResponse(req.user));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
@ -269,12 +268,7 @@ export function updatePassword(req, res) {
|
|||
user.resetPasswordExpires = undefined;
|
||||
|
||||
user.save((saveErr) => {
|
||||
req.logIn(user, loginErr => res.json({
|
||||
email: req.user.email,
|
||||
username: req.user.username,
|
||||
preferences: req.user.preferences,
|
||||
id: req.user._id
|
||||
}));
|
||||
req.logIn(user, loginErr => res.json(userResponse(req.user)));
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -294,13 +288,7 @@ export function saveUser(res, user) {
|
|||
return;
|
||||
}
|
||||
|
||||
res.json({
|
||||
email: user.email,
|
||||
username: user.username,
|
||||
preferences: user.preferences,
|
||||
verified: user.verified,
|
||||
id: user._id
|
||||
});
|
||||
res.json(userResponse(user));
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue