Reputation: 113
DESCRIPTION
This is my first online project and it consist in Vuejs (frontend framework), nodeJs (backend) and mongoDB (database NoSQL).
I'm hosting front in Hostinger, backend also in Hostinger using VPS and MongoDB in MongoDB Atlas.
All frontend, backend and database are working fine, using Localhost or Insomnia/Postman. The CRUD is working well
PROBLEM
It is when I try to access the information from Backend using the real website application.
When I try to do an auth, I got two errors on console:
POST https://89.116.225.159:5000/api/auth/login net::ERR_SSL_PROTOCOL_ERROR
TypeError: Failed to fetch
CODE
This is the code where the fetch does not work. But as I said above, in localhost and inside Imsomnia using the same http path are working just fine
await fetch("http://89.116.225.159:5000/api/auth/login", {
method: "POST",
headers: {"Content-type": "application/json"},
body: jsonDataObject
})
APP This is the app that I created. It is just for training; https://routehelper.com.br/
If you want, you can try to do a login to see the error. user: [email protected] password: mudar123
I have already tried to clear cache, SSL state and adjust the time and date on my computer. Things that I found on internet, but the error still there.
Anyone knows what it could be?
----------------------------edited----------------------------------
BACKEND LOGIN
server.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;
FRONTEND
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,
})
}
})
},
}
Upvotes: 0
Views: 1198
Reputation: 5289
You have some config or infra redirecting your http request to https which is failing because you likely either don't have HTTPS configured or you should be using the domain name instead of the IP as SSL requires a domain name to work(most of the time).
Upvotes: 1