showing results for - "user property in express jwt"
Alice
26 Mar 2018
1jwt({ secret: publicKey, requestProperty'auth' });
Juan
14 Jul 2019
1// JWT MIDDLEWARE
2const jwt = require('jsonwebtoken')
3const httpError = require('http-errors')
4
5module.exports = (req, res, next) => {
6  try {
7    const tokenHeader = req.headers.authorization.split('Bearer ')[1]
8    const decoded = jwt.verify(tokenHeader, process.env.ACCESS_TOKEN_SECRET)
9    req.user = decoded
10    next()
11  } catch (err) {
12    next(httpError(401))
13  }
14}
15
16// ROUTE LOGIN
17app.get('/protect', authJwt, (req, res) => {
18  console.log(req.user)
19  res.send('aim in proteced route')
20})
21
22app.post('/login', (req, res) => {
23  const bodyPayload = {
24    id: Date.now(),
25    username: req.body.username
26  }
27  const token = signAccessToken(res, bodyPayload)
28  return res.status(200).json(token)
29})
30
31app.post('/refresh-token', (req, res) => {
32  const refreshToken = signRefreshToken(req)
33  res.status(200).json(refreshToken)
34  res.end()
35})
36
37// JWT HELPER
38const jwt = require('jsonwebtoken')
39const httpError = require('http-errors')
40
41exports.signAccessToken = (res, payload) => {
42  try {
43    if (payload) {
44      const accessToken = jwt.sign({ ...payload }, process.env.ACCESS_TOKEN_SECRET, { expiresIn: '1m' })
45      const refreshToken = jwt.sign({ ...payload }, process.env.REFRESH_TOKEN_SECRET, { expiresIn: '90d' })
46      res.cookie('refreshToken', `${refreshToken}`, { expired: 86400 * 90 })
47      return { accessToken, refreshToken }
48    }
49  } catch (err) {
50    return httpError(500, err)
51  }
52}
53
54exports.signRefreshToken = (req) => {
55  try {
56    const getToken = req.cookies.refreshToken
57    if (getToken) {
58      const { id, username } = jwt.verify(getToken, process.env.REFRESH_TOKEN_SECRET)
59      const accesssToken = jwt.sign({ id, username }, process.env.ACCESS_TOKEN_SECRET, { expiresIn: '1m' })
60      return { accesssToken }
61    }
62  } catch (err) {
63    return httpError(401, err)
64  }
65}
66
queries leading to this page
express jwt authenticationjwt node js expressexpressjs jwt tokenjwt express node jsimplement jwt in node js express jwtexpress ajwtjwt key expressjsjwt with express 27express jwt examplejwt with express jshow to use jwt expressjwt deecode expressexpress create jwtexpressjwt 28 secret algorithms 27hs256 27 29 unless not workingexpressjwt delete algorithmnjwt node jsnode jwt expressexpress jsonwebtoken examplejwt in express jsexpress jwtinstall jsonwebtoken expresswhat is user property in express jwtjsonwebtoken npmnodejs jwtexpress how to use jwtexpress jwt libraryjwt token expressjwt expressjwt and expresserror 3a cannot find module 27express jwt 27express jwt decode storeexpressjwt middlewre no responseexpress jwt check user typeexpress jwt get datanode express with jwtexpress jwt exampleexpress js with jwtexpress js jwt tokenjwt token in node js expressjwt node js docswhat is express jwtexpress jwt 5cexpress jwt user propertymodule not found jwt expressapp use jwtexpress jwt create tokenexpressjwt 28 7b secret 2c algorithms 3a 5b 27hs256 27 5d 7d 29 unlessjwt verify expressjwt token express jswhat is user property in express jwt meanjwt express middlewareexpress jwt how it worksnpm jsonwebtokennpm i jwt express jwt returnexpress jwt get user from tokenjsonwebtoken npm installjwt system expressuse express jwtexpress auth jwtjwt npmjwt 2bexpressjs express json jwtuser property in express jwtnode js express jwtexpress jwt decode token examplesexpress js express jwtnpm express jwtexpress jwt documentation node jshow to use jwt in expressexpress node jwtadd jwt token expressjsexpress jwttokenjwttoken with express express jwt nodejsapp use jwt expressexpress jwtnodejs express jwtexpress jwt new versionjwt with nodejs expressjwt in expressexpress with jwtjwt for expressexpress js jwtexpress jwt node jsnode js jwt how to use express jwtcheck jwt middlewareimplement jwt in expressexpresss jwtexpress middleware check token npmjwt express jsexpress jwt npmjwt with express jsexpress jwt npmexpress jwt tokenexpress jwt return jsonjwt express documentationjwt token with express jsexpress js jwt authjwt token express jsjwt node jshow to use jwt in express jsexpress jsonwebtoken examplejwt express nodejsexpressjs jwtnode express jwtexpress jsonwebtokenjwt with expressexpress jwt typescript save subject payloadexpressjwt 28 29 3b node jsjwt expressjsjwt node expressjwt docs expressjwt in node js expresshow to implement jwt in expresshwt expressuse express jwt node jsuse express jwt node jsexpress jwt autz npmhow to install jsonwebtoken in expressexpress jwt unlessexpress jwt installnode js express api jwt 22express jwt 22 extract to json filehow to use express jwt in node jsexpress jwt documentationnodejs express jwt examplenode express api jwtexpress jwt npm tutorialnode js express and jwtjwt in expresssjwt express methodsexpress jwt decode tokenexpress jwt extracting jsonrequire 28 27express jwt 27 29 jsonexpress handle jwtcreate jwt token express jwtexpress jwt authentication expressjwtexpress js jwtexpress jwt handleuser property in express jwt