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 passport from 'passport';
|
||||||
|
|
||||||
|
import { userResponse } from './user.controller';
|
||||||
|
|
||||||
export function createSession(req, res, next) {
|
export function createSession(req, res, next) {
|
||||||
passport.authenticate('local', (err, user) => { // eslint-disable-line consistent-return
|
passport.authenticate('local', (err, user) => { // eslint-disable-line consistent-return
|
||||||
if (err) { return next(err); }
|
if (err) { return next(err); }
|
||||||
|
@ -9,28 +11,14 @@ export function createSession(req, res, next) {
|
||||||
|
|
||||||
req.logIn(user, (innerErr) => {
|
req.logIn(user, (innerErr) => {
|
||||||
if (innerErr) { return next(innerErr); }
|
if (innerErr) { return next(innerErr); }
|
||||||
return res.json({
|
return res.json(userResponse(req.user));
|
||||||
email: req.user.email,
|
|
||||||
username: req.user.username,
|
|
||||||
preferences: req.user.preferences,
|
|
||||||
apiKeys: req.user.apiKeys,
|
|
||||||
verified: req.user.verified,
|
|
||||||
id: req.user._id
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
})(req, res, next);
|
})(req, res, next);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getSession(req, res) {
|
export function getSession(req, res) {
|
||||||
if (req.user) {
|
if (req.user) {
|
||||||
return res.json({
|
return res.json(userResponse(req.user));
|
||||||
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.status(404).send({ message: 'Session does not exist' });
|
return res.status(404).send({ message: 'Session does not exist' });
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,17 @@ import {
|
||||||
|
|
||||||
export * from './user.controller/apiKey';
|
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) => {
|
const random = (done) => {
|
||||||
crypto.randomBytes(20, (err, buf) => {
|
crypto.randomBytes(20, (err, buf) => {
|
||||||
const token = buf.toString('hex');
|
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
|
mail.send(mailOptions, (mailErr, result) => { // eslint-disable-line no-unused-vars
|
||||||
res.json({
|
res.json(userResponse(req.user));
|
||||||
email: req.user.email,
|
|
||||||
username: req.user.username,
|
|
||||||
preferences: req.user.preferences,
|
|
||||||
verified: req.user.verified,
|
|
||||||
id: req.user._id
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -224,13 +229,7 @@ export function emailVerificationInitiate(req, res) {
|
||||||
user.verifiedTokenExpires = EMAIL_VERIFY_TOKEN_EXPIRY_TIME; // 24 hours
|
user.verifiedTokenExpires = EMAIL_VERIFY_TOKEN_EXPIRY_TIME; // 24 hours
|
||||||
user.save();
|
user.save();
|
||||||
|
|
||||||
res.json({
|
res.json(userResponse(req.user));
|
||||||
email: req.user.email,
|
|
||||||
username: req.user.username,
|
|
||||||
preferences: req.user.preferences,
|
|
||||||
verified: user.verified,
|
|
||||||
id: req.user._id
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -269,12 +268,7 @@ export function updatePassword(req, res) {
|
||||||
user.resetPasswordExpires = undefined;
|
user.resetPasswordExpires = undefined;
|
||||||
|
|
||||||
user.save((saveErr) => {
|
user.save((saveErr) => {
|
||||||
req.logIn(user, loginErr => res.json({
|
req.logIn(user, loginErr => res.json(userResponse(req.user)));
|
||||||
email: req.user.email,
|
|
||||||
username: req.user.username,
|
|
||||||
preferences: req.user.preferences,
|
|
||||||
id: req.user._id
|
|
||||||
}));
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -294,13 +288,7 @@ export function saveUser(res, user) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
res.json({
|
res.json(userResponse(user));
|
||||||
email: user.email,
|
|
||||||
username: user.username,
|
|
||||||
preferences: user.preferences,
|
|
||||||
verified: user.verified,
|
|
||||||
id: user._id
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue