Next.js Discord

Discord Forum

My cookies are not been set after reload they are gone why ?

Unanswered
Cuvier’s Dwarf Caiman posted this in #help-forum
Open in Discord
Cuvier’s Dwarf CaimanOP
export const login = async (req: Request, res: Response) => {
    try {
        const { email, password } = req.body;
        const user = await prisma.user.findUnique({ where: { email } });

        if (!user) {
            return res.status(400).json({ message: "Invalid credentials" });
        }

        const isValidPassword = await bcrypt.compare(password, user.password);
        if (!isValidPassword) {
            return res.status(400).json({ message: "Invalid credentials" });
        }

        const token = jwt.sign({ userId: user.id }, process.env.JWT_SECRET!);

        await prisma.session.create({
            data: {
                userId: user.id,
                token,
            },
        });

        return res
            .setHeader(
                "Set-Cookie",
                `auth_token=${token}; HttpOnly; Max-Age=3600; Secure; SameSite=Strict;`,
            )
            .status(200)
            .json({ message: "Signin successfully", token });
    } catch (error) {
        return res.status(500).json({ message: "Something went wrong" });
    }
};

my cookies are set on frontend next.js but after i reload they are gone why a little help please !
fontend is hosted on vercel

0 Replies