req.t --> res.t

This commit is contained in:
Matteo Pagliazzi
2015-11-19 12:17:44 +01:00
parent 6451264572
commit dd8c22584d
3 changed files with 17 additions and 18 deletions

View File

@@ -14,8 +14,8 @@ describe('getUserLanguage', () => {
let res, req, next;
let checkReqT = (req) => {
expect(req.t).to.be.a('function');
expect(req.t('help')).to.equal(i18n.t('help', req.language));
expect(res.t).to.be.a('function');
expect(res.t('help')).to.equal(i18n.t('help', req.language));
};
beforeEach(() => {

View File

@@ -31,23 +31,23 @@ api.registerLocal = {
req.checkBody({
username: {
notEmpty: true,
errorMessage: req.t('missingEmail'),
errorMessage: res.t('missingEmail'),
},
email: {
notEmpty: true,
isEmail: true,
errorMessage: req.t('invalidEmail'),
errorMessage: res.t('invalidEmail'),
},
password: {
notEmpty: true,
errorMessage: req.t('missingPassword'),
errorMessage: res.t('missingPassword'),
},
passwordConfirmation: {
notEmpty: true,
equals: {
options: [req.body.password],
},
errorMessage: req.t('passwordConfirmationMatch'),
errorMessage: res.t('passwordConfirmationMatch'),
},
});
@@ -85,9 +85,9 @@ api.registerLocal = {
])
.then((results) => {
if (results[0]) {
if (email === results[0].auth.local.email) return next(new NotAuthorized(req.t('emailTaken')));
if (email === results[0].auth.local.email) return next(new NotAuthorized(res.t('emailTaken')));
// Check that the lowercase username isn't already used
if (lowerCaseUsername === results[0].auth.local.lowerCaseUsername) return next(new NotAuthorized(req.t('usernameTaken')));
if (lowerCaseUsername === results[0].auth.local.lowerCaseUsername) return next(new NotAuthorized(res.t('usernameTaken')));
}
let salt = passwordUtils.makeSalt();
@@ -152,11 +152,11 @@ api.loginLocal = {
req.checkBody({
username: {
notEmpty: true,
errorMessage: req.t('missingUsernameEmail'),
errorMessage: res.t('missingUsernameEmail'),
},
password: {
notEmpty: true,
errorMessage: req.t('missingPassword'),
errorMessage: res.t('missingPassword'),
},
});
@@ -184,7 +184,7 @@ api.loginLocal = {
// TODO place back long error message return res.json(401, {err:"Uh-oh - your username or password is incorrect.\n- Make sure your username or email is typed correctly.\n- You may have signed up with Facebook, not email. Double-check by trying Facebook login.\n- If you forgot your password, click \"Forgot Password\"."});
let isValidPassword = user && user.auth.local.hashed_password !== passwordUtils.encrypt(req.body.password, user.auth.local.salt);
if (!isValidPassword) return next(new NotAuthorized(req.t('invalidLoginCredentials')));
if (!isValidPassword) return next(new NotAuthorized(res.t('invalidLoginCredentials')));
res
.status(200)

View File

@@ -56,9 +56,8 @@ function _getFromUser (user, req) {
return lang;
}
function _attachTranslateFunction (req, next) {
// TODO attach to res?
req.t = function reqTranslation () {
function _attachTranslateFunction (req, res, next) {
res.t = function reqTranslation () {
return i18n.t(...arguments, req.language);
};
@@ -68,10 +67,10 @@ function _attachTranslateFunction (req, next) {
export default function getUserLanguage (req, res, next) {
if (req.query.lang) { // In case the language is specified in the request url, use it
req.language = translations[req.query.lang] ? req.query.lang : 'en';
return _attachTranslateFunction(req, next);
return _attachTranslateFunction(...arguments);
} else if (req.locals && req.locals.user) { // If the request is authenticated, use the user's preferred language
req.language = _getFromUser(req.locals.user, req);
return _attachTranslateFunction(req, next);
return _attachTranslateFunction(...arguments);
} else if (req.session && req.session.userId) { // Same thing if the user has a valid session
User.findOne({
_id: req.session.userId,
@@ -79,11 +78,11 @@ export default function getUserLanguage (req, res, next) {
.exec()
.then((user) => {
req.language = _getFromUser(user, req);
return _attachTranslateFunction(req, next);
return _attachTranslateFunction(...arguments);
})
.catch(next);
} else { // Otherwise get from browser
req.language = _getFromUser(null, req);
return _attachTranslateFunction(req, next);
return _attachTranslateFunction(...arguments);
}
}