身份验证令牌未生成

时间:2020-08-24 08:57:11

标签: node.js express jwt

我正在使用JsonWebToken和邮递员在快递服务器中发送请求。

期望通过电子邮件和密码查看令牌,但向我显示捕获短语的错误错误:“尝试登录时发生错误”。

验证码

const { User } = require('../models')
const jwt = require('jsonwebtoken')
const config = require('../config/config')

function jwtSignUser (user) {
  const ONE_WEEK = 60 * 60 * 24 * 7
  return jwt.sign(user, config.authentication.jwtSecret, {
    expriesIn: ONE_WEEK
  })
}
module.exports = {
  async register (req, res) {
    try {
      const user = await User.create(req.body)
      res.send(user.toJSON())
    } catch (err) {
      res.status(400).send({
        error: 'This email account is already in use.'
      })
    }
  },
  async login (req, res) {
    try {
      const { email, password } = req.body
      const user = await User.findOne({
        where: {
          email: email
        }
      })
      if (!user) {
        return res.status(403).send({
          error: 'The login information was incorrect'
        })
      }
      const isPasswordValid = password === user.password
      console.log(isPasswordValid)
      if (!isPasswordValid) {
        return res.status(403).send({
          error: 'The login information was incorrect'
        })
      }
      const userJson = user.toJSON()
      console.log(userJson)
      res.send({
        user: userJson,
        token: jwtSignUser(userJson)
      })
    } catch (error) {
console.log(error)
      res.status(500).send({
        error: 'An error has occured trying to login'
      })
    }
  }// module export end
}

config.js是包含数据库配置和身份验证配置的文件

module.exports = {
  port: process.env.PORT || 8081,
  db: {
    database: process.env.DB_NAME || 'tabtracker',
    user: process.env.DB_USER || 'tabtracker',
    password: process.env.DB_PASS || 'tabtracker',
    options: {
      dialect: process.env.DIALECT || 'sqlite',
      host: process.env.HOST || 'localhost',
      storage: './tabtracker.sqlite'
    }
  },
  authentication: {
    jwtSecret: process.env.JWT_SECRET || 'secret'
  }
}

控制台信息

Executing (default): SELECT `id`, `email`, `password`, `createdAt`, `updatedAt` FROM `Users` AS `User` WHERE `User`.`email` = 'jhgjhg@gmail.com';
true
{
  id: 2,
  email: 'jhgjhg@gmail.com',
  password: 'passw0rd',
  createdAt: 2020-08-24T08:45:07.284Z,
  updatedAt: 2020-08-24T08:45:07.284Z
}
Error: "expriesIn" is not allowed in "options"
    at D:\DATA\Learning\Creations\WEB\website\tabs_tracker\server\node_modules\jsonwebtoken\sign.js:47:17
    at Array.forEach (<anonymous>)
    at validate (D:\DATA\Learning\Creations\WEB\website\tabs_tracker\server\node_modules\jsonwebtoken\sign.js:43:6)
    at validateOptions (D:\DATA\Learning\Creations\WEB\website\tabs_tracker\server\node_modules\jsonwebtoken\sign.js:58:10)
    at Object.module.exports [as sign] (D:\DATA\Learning\Creations\WEB\website\tabs_tracker\server\node_modules\jsonwebtoken\sign.js:141:5)
    at jwtSignUser (D:\DATA\Learning\Creations\WEB\website\tabs_tracker\server\src\controllers\AuthencationController.js:7:14)
    at login (D:\DATA\Learning\Creations\WEB\website\tabs_tracker\server\src\controllers\AuthencationController.js:46:16)
combine

1 个答案:

答案 0 :(得分:0)

我可以看到其中有一个expriesIn的错字,应该是expiresIn