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 res, req, next;
let checkReqT = (req) => { let checkReqT = (req) => {
expect(req.t).to.be.a('function'); expect(res.t).to.be.a('function');
expect(req.t('help')).to.equal(i18n.t('help', req.language)); expect(res.t('help')).to.equal(i18n.t('help', req.language));
}; };
beforeEach(() => { beforeEach(() => {

View File

@@ -31,23 +31,23 @@ api.registerLocal = {
req.checkBody({ req.checkBody({
username: { username: {
notEmpty: true, notEmpty: true,
errorMessage: req.t('missingEmail'), errorMessage: res.t('missingEmail'),
}, },
email: { email: {
notEmpty: true, notEmpty: true,
isEmail: true, isEmail: true,
errorMessage: req.t('invalidEmail'), errorMessage: res.t('invalidEmail'),
}, },
password: { password: {
notEmpty: true, notEmpty: true,
errorMessage: req.t('missingPassword'), errorMessage: res.t('missingPassword'),
}, },
passwordConfirmation: { passwordConfirmation: {
notEmpty: true, notEmpty: true,
equals: { equals: {
options: [req.body.password], options: [req.body.password],
}, },
errorMessage: req.t('passwordConfirmationMatch'), errorMessage: res.t('passwordConfirmationMatch'),
}, },
}); });
@@ -85,9 +85,9 @@ api.registerLocal = {
]) ])
.then((results) => { .then((results) => {
if (results[0]) { 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 // 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(); let salt = passwordUtils.makeSalt();
@@ -152,11 +152,11 @@ api.loginLocal = {
req.checkBody({ req.checkBody({
username: { username: {
notEmpty: true, notEmpty: true,
errorMessage: req.t('missingUsernameEmail'), errorMessage: res.t('missingUsernameEmail'),
}, },
password: { password: {
notEmpty: true, 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\"."}); // 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); 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 res
.status(200) .status(200)

View File

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