繁体   English   中英

ERR_SSL_PROTOCOL_ERROR | 完整应用(背面+正面)

[英]ERR_SSL_PROTOCOL_ERROR | Full aplication (back + front)

描述

这是我的第一个在线项目,它包含Vuejs(前端框架)、nodeJs(后端)和mongoDB(数据库NoSQL)。

我在 Hostinger 中托管前端,后端也在 Hostinger 中使用 VPS 和 MongoDB Atlas 中的 MongoDB。

使用 Localhost 或 Insomnia/Postman,所有前端、后端和数据库都工作正常。 CRUD 运行良好

问题

这是当我尝试使用真实的网站应用程序从后端访问信息时。

当我尝试进行身份验证时,控制台出现两个错误:

POST https://89.116.225.159:5000/api/auth/login net::ERR_SSL_PROTOCOL_ERROR
TypeError: Failed to fetch

代码

这是提取不起作用的代码。 但正如我上面所说,在本地主机和 Imsomnia 内部使用相同的 http 路径工作得很好

await fetch("http://89.116.225.159:5000/api/auth/login", {
    method: "POST",
    headers: {"Content-type": "application/json"},
    body: jsonDataObject
})

APP这是我创建的应用程序。 它只是为了训练; https://routehelper.com.br/

如果需要,您可以尝试登录以查看错误。 用户:vitin@hotmail.com 密码:mudar123

我已经尝试清除缓存 SSL state 并调整计算机上的时间和日期。 我在 inte.net 上找到的东西,但错误仍然存在。

任何人都知道它可能是什么?

--------------------------编辑-------------------- --------------

后台登录

服务器.js

require('dotenv').config()
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const cors = require("cors");

// routes
const authRouter = require("./routes/authRoutes");

// config
const dbName = "databaseRoutering"
const port = 5000;

const DB_USER = process.env.DB_USER
const DB_PASS = process.env.DB_PASS

const app = express();

app.use(cors());
app.use(express.json());
app.use(express.static("public"));

app.use("/api/auth", authRouter)

app.get("/", (req, res) => {
    res.json({ message: "Rota teste"})
})

// mongoDB connection
mongoose.connect(
    `mongodb+srv://${DB_USER}:${DB_PASS}@cluster0.06ovsjg.mongodb.net/users?retryWrites=true&w=majority`,
);

app.listen(port, () => {
    console.log(`Nossa variável ${process.env.MY_VARIABLE}`)
    console.log(`backend rodando na porta ${port}`)
})

authRoutes.js

const router = require("express").Router();
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");

const User = require("../models/user");

router.post("/login", async (req, res) => {
    
    const email = req.body.email;
    const password = req.body.password;

    const user = await User.findOne({ email: email });

    if(!user){
        return res.status(400).json({ error: "Não há um usuário cadastrado com esse email!" })
    }

    // check if password match
    const checkPassword = await bcrypt.compare(password, user.password);

    if(!checkPassword){
        return res.status(400).json({ error: "Senha Inválida!"});
    }

    // create token
    const token = jwt.sign(
        {
            name: user.name,
            id: user._id
        },
        "oursecret"
    );

    // return token
    res.json({ error: null, msg: "Você está autenticado!", 
        token: token, 
        userId: user._id, 
        firstName: user.firstName, 
        lastName: user.lastName, 
        email: user.email,
    })
})

module.exports = router;

前端

methods: {
        changeToRegister() {
                this.$store.commit('changeToRegister')
        },
        async loginVerification(e) {
            // it does not let the page reaload
            e.preventDefault();
            // it creates the object that will be use on API
            const dataObject = {
                email: this.auth.email,
                password: this.auth.password
            }
            const jsonDataObject = JSON.stringify(dataObject)
            await fetch("http://89.116.225.159:5000/api/auth/login", {
                method: "POST",
                headers: {"Content-type": "application/json"},
                body: jsonDataObject
            })
            .then((resp) => resp.json())
            // it access the api to update the profile data using token and the object
            .then((data) => {
                if(data.error){
                    // it prints the error
                    this.returnMessage = data.error;
                } else {
                    // it takes to the dashboard page and commit all the page with the user info
                    this.$router.push({ path: '/ClientPage' })
                    this.$store.commit("authenticate", {
                        token: data.token, 
                        userId: data.userId, 
                        firstName: data.firstName, 
                        lastName: data.lastName,
                        email: data.email,
                    })
                }
            })
        },
    }

您有一些配置或基础设施将您的 http 请求重定向到 https 失败,因为您可能没有配置 HTTPS 或者您应该使用域名而不是 IP 因为 SSL 需要域名才能工作(大部分时间).

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM