Skip to content
This repository has been archived by the owner on Jan 1, 2024. It is now read-only.

Commit

Permalink
feat(server/middlewares): add i18n stuff to auth middleware
Browse files Browse the repository at this point in the history
feat(server/middlewares): add i18n stuff to auth middleware
  • Loading branch information
Metnew committed Sep 28, 2017
1 parent 34a5f91 commit 9db4b9e
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/server/middlewares/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}

Expand All @@ -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
}
}
Expand Down

0 comments on commit 9db4b9e

Please sign in to comment.