diff --git a/src/server/middlewares/auth.js b/src/server/middlewares/auth.js index a66df4fe..6bed74a5 100644 --- a/src/server/middlewares/auth.js +++ b/src/server/middlewares/auth.js @@ -3,6 +3,7 @@ */ import chalk from 'chalk' import jwt from 'jsonwebtoken' +import {supportedLanguages, defaultLanguage} from '../i18n' import {JWT_TOKEN} from 'common/api/LocalStorageCookiesSvc' /** * Auth-related middleware. @@ -11,10 +12,15 @@ import {JWT_TOKEN} from 'common/api/LocalStorageCookiesSvc' * @param {Response} res * @param {Function} next */ -export default (req: express$Request, res: express$Response, next: () => void) => { +export default ( + req: express$Request, + res: express$Response, + next: () => void +) => { + const language: string = req.acceptsLanguages(supportedLanguages) || defaultLanguage req.user = { token: null, - lang: 'en', + language, isLoggedIn: false } @@ -23,15 +29,17 @@ export default (req: express$Request, res: express$Response, next: () => void) = return next() } - console.log(chalk.blue('USER HAS TOKEN')) + console.log(chalk.blue('USER HAS JWT TOKEN')) jwt.verify(token, process.env.JWT_SECRET, (err, decoded) => { if (err) { console.log(chalk.red('CANT DECODE JWT TOKEN!', err)) } else { console.log(chalk.magenta('TOKEN SUCCESSFULLY DECODED')) + // NOTE: set user language in jwt token that may helps handling i18n efficiently + const {username} = decoded req.user = { - ...decoded, token, + username, isLoggedIn: true } }