首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Express-jwt未返回任何响应

Express-jwt未返回任何响应
EN

Stack Overflow用户
提问于 2021-03-31 22:58:09
回答 1查看 63关注 0票数 1

我正在尝试使用express-jwt和我的app.js文件中的中间件函数来创建登录功能。但是每当我尝试使用postman发送get请求时,它发送请求的时间是无限的,并且从不返回任何错误或成功消息。我使用dynamoDB作为数据库。

这是我的Login.js文件

代码语言:javascript
复制
const AWS = require("aws-sdk");
const express = require("express");
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");
require("dotenv").config();

AWS.config.update({ region: "us-east-2" });

const docClient = new AWS.DynamoDB.DocumentClient();
const router = express.Router();

router.post("/login", (req, res) => {
  user_type = "customer";
  const email = req.body.email;
  docClient.get(
    {
      TableName: "users",
      Key: {
        user_type,
        email,
      },
    },
    (err, data) => {
      if (err) {
        res.send("Invalid username or password");
      } else {
        if (data && bcrypt.compareSync(req.body.password, data.Item.password)) {
          const token = jwt.sign(
            {
              email: data.Item.email,
            },
            process.env.SECRET,
            { expiresIn: "1d" }
          );
          res.status(200).send({ user: data.Item.email, token: token });
        } else {
          res.status(400).send("Password is wrong");
        }
      }
    }
  );
});

module.exports = router;

这是我的jwt.js文件:

代码语言:javascript
复制
const expressJwt = require("express-jwt");
require("dotenv").config();

function authJwt() {
  const secret = process.env.SECRET;
  return expressJwt({
    secret,
    algorithms: ["HS256"],
  });
}

module.exports = authJwt;

我试着在我的app.js文件中像这样使用expressJwt

代码语言:javascript
复制
app.use(authJwt); //If I'm not using this, then the code works fine without API protection

有人能告诉我我的代码出了什么问题吗?感谢你方的任何帮助。

EN

回答 1

Stack Overflow用户

发布于 2021-07-21 04:40:13

从你的jwt.js中删除函数,它应该看起来像这样

代码语言:javascript
复制
const expressJwt = require('express-jwt');
const secret = process.env.secret

const authJwt = expressJwt({
secret,
algorithms:['HS256']
 })
module.exports = authJwt;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66890288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档