add req.t in place of i18n.t passing req.language, begins implementing user signup

This commit is contained in:
Matteo Pagliazzi
2015-11-18 22:04:36 +01:00
parent 662c9ecc29
commit 5c859ca52e
4 changed files with 66 additions and 20 deletions
+26 -16
View File
@@ -1,6 +1,3 @@
import i18n from '../../../../common/script/i18n';
// TODO add getUserLanguage as a global middleware?
import getUserLanguage from '../../middlewares/api-v3/getUserLanguage';
import validator from 'validator';
import {
NotAuthorized,
@@ -11,39 +8,52 @@ import User from '../../models/user';
let api = {};
/**
* @api {get} /user/login/local Login a user with email / username and password
* @api {post} /user/register/local Register a new user with email, username and password
* @apiVersion 3.0.0
* @apiName UserRegisterLocal
* @apiGroup User
*
* @apiParam {String} username Username of the new user
* @apiParam {String} email Email address of the new user
* @apiParam {String} password Password for the new user account
* @apiParam {String} passwordConfirmation Password confirmation
*
* @apiSuccess {Object} user The user public fields
*
*
* @apiUse NotAuthorized
*/
api.registerLocal = {
method: 'POST',
url: '/user/register/local',
};
/**
* @api {post} /user/login/local Login an user with email / username and password
* @apiVersion 3.0.0
* @apiName UserLoginLocal
* @apiGroup User
*
* @apiParam {String} username Username or email of the User.
* @apiParam {String} username Username or email of the user
* @apiParam {String} password The user's password
*
* @apiSuccess {String} _id The user's unique identifier
* @apiSuccess {String} apiToken The user's api token that must be used to authenticate requests.
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
* {
* "_id": "f47ac10b-58cc-4372-a567-0e02b2c3d479",
* "apiToken": "1234567890"
* }
*
* @apiUse NotAuthorized
*/
api.loginLocal = {
method: 'POST',
url: '/user/login/local',
middlewares: [getUserLanguage],
handler (req, res, next) {
req.checkBody({
username: {
notEmpty: true,
errorMessage: i18n.t('missingUsernameEmail'),
errorMessage: req.t('missingUsernameEmail'),
},
password: {
notEmpty: true,
errorMessage: i18n.t('missingPassword'),
errorMessage: req.t('missingPassword'),
},
});
@@ -71,7 +81,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(i18n.t('invalidLoginCredentials')));
if (!isValidPassword) return next(new NotAuthorized(req.t('invalidLoginCredentials')));
res
.status(200)
@@ -1,5 +1,6 @@
import { model as User } from '../../models/user';
import accepts from 'accepts';
import { i18n } from '../../../../common';
import _ from 'lodash';
import {
translations,
@@ -55,13 +56,21 @@ function _getFromUser (user, req) {
return lang;
}
function _attachTranslateFunction (req, next) {
req.t = function reqTranslation () {
return i18n.t(...arguments, req.language);
};
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 next();
return _attachTranslateFunction(req, next);
} 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 next();
return _attachTranslateFunction(req, next);
} else if (req.session && req.session.userId) { // Same thing if the user has a valid session
User.findOne({
_id: req.session.userId,
@@ -69,11 +78,11 @@ export default function getUserLanguage (req, res, next) {
.exec()
.then((user) => {
req.language = _getFromUser(user, req);
return next();
return _attachTranslateFunction(req, next);
})
.catch(next);
} else { // Otherwise get from browser
req.language = _getFromUser(null, req);
return next();
return _attachTranslateFunction(req, next);
}
}
+2
View File
@@ -1,5 +1,6 @@
// This module is only used to attach middlewares to the express app
import expressValidator from 'express-validator';
import getUserLanguage from './getUserLanguage';
import analytics from './analytics';
import errorHandler from './errorHandler';
import bodyParser from 'body-parser';
@@ -21,6 +22,7 @@ export default function attachMiddlewares (app) {
app.use(bodyParser.json());
app.use(expressValidator()); // TODO config
app.use(analytics);
app.use(getUserLanguage);
app.use('/api/v3', routes);
app.use(notFoundHandler);