简体   繁体   English

POST http://localhost:5000/app/auth/login 500(内部服务器错误)

[英]POST http://localhost:5000/app/auth/login 500 (Internal Server Error)

Hi i am newbie about this stuff please help me whenever I try to authenticate i am getting this error嗨,我是这个东西的新手,每当我尝试进行身份验证时,请帮助我,我收到此错误

https://i.stack.imgur.com/VqQRY.png https://i.stack.imgur.com/VqQRY.png

my code is here:我的代码在这里:

index指数

const express = require('express')
const cors = require('cors')
const mongoose = require('mongoose')
const dotenv = require('dotenv')
const authRoute = require('./routes/Auth')
const userRoute = require('./routes/User')
const examRoute = require('./routes/Exam')
const examQuestionsRoute = require('./routes/ExamQuestions')
const userExamsRoute = require('./routes/UserExams')

dotenv.config();
const app = express()
app.use(cors())
app.use(express.json())

mongoose.connect(process.env.DATABASE_ACCESS, () => console.log("db connected 
succes"))

app.use("/app/auth", authRoute);
app.use("/app/exam", examRoute);
app.use("/app/examquestions", examQuestionsRoute);
app.use("/app/userexams", userExamsRoute);

app.listen(5000, () => {
 console.log('Server started on 5000')
})

Auth router认证路由器

const router = require("express").Router();
const User = require("../models/User");
const CryptoJS = require("crypto-js");

router.post("/login", async (req, res) => {
  try {
    const user = await User.findOne({ email: req.body.email });
    if (!user) {
      errors.email = "Wrong credentials!";
      res.status(404).json({ errors });
      return;
    }

    const hashedPassword = CryptoJS.AES.decrypt(
      user.password,
      process.env.PASS_SEC
    );
    const OriginalPassword = hashedPassword.toString(CryptoJS.enc.Utf8);

    OriginalPassword !== req.body.password &&
      res.status(401).json("Wrong credentials!");

    const { password, ...others } = user._doc;
    res.status(200).json({...others});
  } catch (err) {
    res.status(500).json(err);
  }
});
module.exports = router;

I do not know if it is necessary since code 500 is server side issue but the "client" code is:我不知道是否有必要,因为代码 500 是服务器端问题,但“客户端”代码是:

import {useState } from 'react'
import { useNavigate } from "react-router-dom";
import { useDispatch, useSelector } from "react-redux";
import { login } from "../redux/apiCalls";

const Login = () => {

  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const dispatch = useDispatch();

  const navigate = useNavigate()

  const handleLogin = (e) => {
    e.preventDefault();
    login(dispatch, { email, password });
  };

return ()}

redux api calls redux api 来电

import { publicRequest } from "../requestMethods";

export const login = async (dispatch, user) => {
  dispatch(loginStart());
  try {
    const res = await publicRequest.post("/auth/login", user);
    dispatch(loginSuccess(res.data));
  } catch (err) {
    dispatch(loginFailure());
  }
};

requestmethod.js is requestmethod.js 是

export const publicRequest = axios.create({baseURL: http://localhost:5000/app/,});

i am waiting for good news guys thanks for the attention!我在等待好消息,谢谢大家的关注!

my terminals:我的终端:

https://i.stack.imgur.com/SnJFG.png https://i.stack.imgur.com/SnJFG.png

i guess i found the error:我想我发现了错误:

my error log我的错误日志

i have been working on this error for 3 days, i am open to any advice我已经为这个错误工作了 3 天,我愿意接受任何建议

I think your issue is due to the fact that you're sending the request two times in your code:我认为您的问题是由于您在代码中发送了两次请求:

OriginalPassword !== req.body.password &&
  res.status(401).json("Wrong credentials!");

const { password, ...others } = user._doc;
res.status(200).json({...others});

Here you have res.status(401).json(...) and res.status(200).json(...) which might be executed one after the other.这里有res.status(401).json(...)res.status(200).json(...)可能一个接一个地执行。 When that happens you're trying to rewrite on a response that has already been sent with the json() method, which eventually throws the error you're having发生这种情况时,您会尝试重写已使用json()方法发送的响应,这最终会引发您遇到的错误

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 POST http://localhost:5000/users/signin 500(内部服务器错误)试图使身份验证工作 - POST http://localhost:5000/users/signin 500 (Internal Server Error) trying to make auth works POST http:// localhost:3000 / upload 500(内部服务器错误) - POST http://localhost:3000/upload 500 (Internal Server Error) 无法登录,我收到 POST http://localhost:5000/auth/login 404 (Not Found) 和 net::ERR_SSL_PROTOCOL_ERROR - Cannot login, and i getting POST http://localhost:5000/auth/login 404 (Not Found) and net::ERR_SSL_PROTOCOL_ERROR 如何修复 POST http://localhost:8000/cour 500(内部服务器错误)laravel、ajax 和 Yajra 包 - how to fix POST http://localhost:8000/cour 500 (Internal Server Error) laravel,ajax and Yajra pakage isAxiosError.js:12 POST http://localhost:3000/cartdata 500(内部服务器错误) - isAxiosError.js:12 POST http://localhost:3000/cartdata 500 (Internal Server Error) POST http://localhost:3000/api/v1/register 500(内部服务器错误) - POST http://localhost:3000/api/v1/register 500 (Internal Server Error) POST http://localhost:3000/api/v1/register 500(内部服务器错误)xhr.js:220 - POST http://localhost:3000/api/v1/register 500 (Internal Server Error) xhr.js:220 POST https://localhost:44351/Common/SearchAvailableTechList 500(内部服务器错误) - POST https://localhost:44351/Common/SearchAvailableTechList 500(Internal Server Error) Auth0 axios.post 带有令牌内部服务器错误 500 - Auth0 axios.post with token Internal Server Error 500 $ http.post到php文件,500内部服务器错误 - $http.post to php file, 500 internal server error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM