I used the req.session.set("user", ...data) and it saved when I checked. Now the problem is with the getServerSideProps.
This is my TSX getServerSideProps code
export const getServerSideProps: GetServerSideProps =
withSession(unauthenticatedRoute);
This is the session code
import { withIronSession } from "next-iron-session";
import { NextIronHandler, NextRoute } from "../typings/types";
export function withSession(handler: NextIronHandler | NextRoute) {
return withIronSession(handler, {
password: process.env.COOKIE_SECRET,
cookieName: "session",
ttl: 15 * 24 * 3600,
cookieOptions: {
secure: process.env.NODE_ENV === "production",
sameSite: "strict",
httpOnly: true,
},
});
}
And this is the route code
export async function unauthenticatedRoute(
ctx: GetServerSidePropsContext & { req: { session: Session } }
) {
const user = ctx.req.session.get("user");
return {
props: user ? { user } : {},
};
}
function HomePage({ user }: Props) {
const router = useRouter();
useEffect(() => {
if (router.query.r) {
location.replace("/");
}
console.log(user)
}, []);
}
It logged undefined, does anyone knows why?