Same as Title says, Why does my Get request sometimes work, but most of the time it 404's?
Been messing around with taking the "next()" out, but it doesnt matter same effect. Same with making the "res.json(req.user.firstName);" in the -----router.get("/user, auth, (req,res){})-----
module.exports.isAuth = (req, res, next) => {
if (req.isAuthenticated()) {
console.log(req.user);
res.json(req.user.firstName);
//next()
} else {
console.log("Nope");
res
.status(401)
.json({ msg: "You are not authorized to view this resource" });
}
};
when the get request DOES work, the log on the backend here ^^^^ gets logged twice, in 2 different ways like bwloe. nice/pretty and ugly.
{
_id: 60e9fad2837ff62fcb580f27,
username: 'drakecoleman@rocketmail.com',
firstName: 'Drake',
lastName: 'Coleman',
hash: 'e017095b5684f72d88d9103f3a5df9dfbcb8fe23e823b6f0551a20151710721304b58cb02bc62b60098a383d0f573fdf5bcd30aa89a5b266db0d7aceb6d25656',
salt: '7e040e90411d647200cc9cc120a1b3b182b035105ce3488fbcbfd39b56f44d9b',
admin: true,
__v: 0
}
{"_id":"60e9fad2837ff62fcb580f27","username":"drakecoleman@rocketmail.com","firstName":"Drake","lastName":"Coleman","hash":"e017095b5684f72d88d9103f3a5df9dfbcb8fe23e823b6f0551a20151710721304b58cb02bc62b60098a383d0f573fdf5bcd30aa89a5b266db0d7aceb6d25656","salt":"7e040e90411d647200cc9cc120a1b3b182b035105ce3488fbcbfd39b56f44d9b","admin":true,"__v":0}
When it 404's, the log only logs the ugly version of the user on the backend.
index.js
const router = require("express").Router();
const passport = require("passport");
const bodyParser = require("body-parser");
const genPassword = require("../lib/passwordUtils").genPassword;
const connection = require("../config/database");
const mongoose = require("mongoose");
const User = mongoose.models.User;
const isAuth = require("./authMiddleware").isAuth;
router.use(bodyParser.urlencoded({ extended: false }));
/**
* -------------- GET ROUTES ----------------
*
*/
router.get("/user", isAuth);
React Front End
user.js
import React from "react";
// import { Redirect } from "react-router-dom";
function UserProfile() {
// if (props.auth === true) {
// return <Redirect to="/login" />;
// } else {
// return <h1 className="red">Logged in </h1>;
// }
fetch("http://localhost:3000/user", {
method: "GET",
credentials: "include",
withCredentials: true,
headers: {
"Content-Type": "application/json",
},
})
.then((res) => res.json())
.then((data) => console.log(data))
.catch((err) => console.log(err));
return <h1>Hello</h1>;
}
export default UserProfile;
This is in the console on the front end before the request, but i dont think its an issue since i get this on other routes with no issue "[HMR] Waiting for update signal from WDS..."
but I do get this error first on the front end console when it 404's
user.js:10 GET http://localhost:3000/user 404 (Not Found)
then after a few seconds I get (its also not typed in red background either, just white text)
TypeError: Failed to fetch user.js:10 Fetch failed loading: GET "http://localhost:3000/user".
Someone on the discord says to make sure I have the sessions initliazed and I do, on the app.js backend page. app.use(passport.initialize()); app.use(passport.session());