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

Commit

Permalink
feat(src/server): formatting + add expiresIn for JWT
Browse files Browse the repository at this point in the history
  • Loading branch information
Metnew committed Aug 7, 2017
1 parent 4a57bc3 commit d1fc014
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
33 changes: 17 additions & 16 deletions src/server/api/auth/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import {Router} from 'express'
import jwt from 'jsonwebtoken'
import chalk from 'chalk'
// import validator from 'validator'
import {JWT_TOKEN} from 'common/api'
// Import validator from 'validator'
// {isLength, trim, isAlphanumeric, escape}
const router = Router()

// define the home page route
// Define the home page route
router.post('/', (req, res) => {
// const {username, password} = req.body
// const usernameValidated = validator.isLength(0, 36).is
// const passwordValidated =
const data = {username: 'cool_username_for_testing'}
jwt.sign(data, process.env.JWT_SECRET, (err, token) => {
if (err) {
throw new Error(
`Cant create JWT token based on input data: ${JSON.stringify(data)}`,
err
)
}
console.log(chalk.yellow(`Generated token for user: ${data.username}`))
res.json({token})
})
// Const {username, password} = req.body
// const usernameValidated = validator.isLength(0, 36).is
// const passwordValidated =
const data = {username: 'cool_username_for_testing'}
jwt.sign(data, process.env.JWT_SECRET, {expiresIn: '7d'}, (err, token) => {
if (err) {
throw new Error(
`Cant create JWT token based on input data: ${JSON.stringify(data)}`,
err
)
}
console.log(chalk.yellow(`Generated token for user: ${data.username}`))
res.json({token})
})
})

export default router
52 changes: 27 additions & 25 deletions src/server/express/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,45 @@ import {JWT_TOKEN} from 'common/api'

const {DIST_PATH, JWT_SECRET} = process.env
const app = express()
// add express stuff
// Add express stuff
app.use(helmet())
app.use(compression())
app.use(morgan('dev'))
app.use(cookieParser())
app.use(
express.static(DIST_PATH, {
// don't use index.html inside /dist dir
index: false
})
express.static(DIST_PATH, {
// Don't use index.html inside /dist dir
index: false
})
)
app.use(bodyParser.json())
app.disable('x-powered-by')

// Auth-related middleware, check that user is logged in and token is valid
app.use((req, res, next) => {
req.user = {}
const token = req.cookies[JWT_TOKEN]
if (!token) {
return next()
}
req.user = {}
const token = req.cookies[JWT_TOKEN]
if (!token) {
return next()
}

jwt.verify(token, JWT_SECRET, (err, decoded) => {
if (err) {
console.log(chalk.red('CANT DECODE JWT TOKEN!', err))
} else {
req.user = {
...decoded,
token,
isLoggedIn: true
}
}
console.log(
chalk.blue(`USER IS LOGGED IN: ${req.user.isLoggedIn ? 'YES' : 'NO'}`)
)
next()
})
console.log(chalk.blue('USER HAS TOKEN'))
jwt.verify(token, JWT_SECRET, (err, decoded) => {
if (err) {
console.log(chalk.red('CANT DECODE JWT TOKEN!', err))
} else {
console.log(chalk.magenta('TOKEN SUCCESSFULLY DECODED'))
req.user = {
...decoded,
token,
isLoggedIn: true
}
}
console.log(
chalk.yellow(`USER IS LOGGED IN: ${req.user.isLoggedIn ? 'YES' : 'NO'}`)
)
next()
})
})

export default app

0 comments on commit d1fc014

Please sign in to comment.