const jwt = require("jsonwebtoken")

const authenticate = async (req, res, next) => {
  var token = req.headers.authorization
  if(!token){
    //throw new Error("undefined token")
    
  }else{
    token =token.split(' ')[1]
  }
  console.log("******************************",token)
  try {
    const verified = jwt.verify(token, "123454")
    req.verifiedUser = verified.user
    console.log("++++++++++++++++++++++++++++++++++++++++++++++",req.verifiedUser)
    console.log("Verification success!", verified)
    next()
  } catch (err) {
    console.log("Verification failed!", err)
    next()
  }
}

module.exports =  authenticate 