I can fetch and get logged in user data without using serverside props but I can't get the data using getServerSideProps in next js. I think the reason is. it is not sending cookies with get request.
frontend
import axios from "axios";
import React, { useEffect } from "react";
const Login = () => {
// perfectly working.
useEffect(() => {
const getUser = async () => {
const { data } = await axios.get(
"http://localhost:5000/auth/login/success",
{
withCredentials: true,
}
);
console.log(data.user);
};
getUser();
}, []);
const google = () => {
try {
window.open(`http://localhost:5000/auth/google`, "_self");
} catch (error) {}
};
return <button onClick={google}>Login with Google</button>;
};
export default Login;
// do not work
export async function getServerSideProps(context) {
try {
const { data } = await axios.get(
"http://localhost:5000/auth/login/success",
{
withCredentials: true,
}
);
console.log(data);
return {
props: {
user: data.user,
},
};
} catch (error) {
return {
props: {
user: false,
},
};
}
}
//backend code
router.get("/login/success", (req, res) => {
console.log(req.user);
if (req.user) {
res.status(200).json({
success: true,
message: "successfull",
isLoggedIn: true,
user: req.user,
});
} else {
res.status(401).json({
success: false,
message: "Unauthorized: User is not logged in",
});
}
});
is there any way to include my cookies when sending a get request from server side props?