Send cookie after next-auth login
Unanswered
Cuvier’s Dwarf Caiman posted this in #help-forum
Cuvier’s Dwarf CaimanOP
I am trying to set cookies after succesfull credential login using next auth
This is the full authorize function:
setCookie({ res }, "name", "value", {
maxAge: 2 * 24 * 60 * 60,
path: "/",
httpOnly: true,
});
if (user.user.isAdmin) {
console.log("set cookie");
setCookie({ res }, "userIsAdmin", "1", {
maxAge: 60 * 60,
httpOnly: true,
path: "/",
});
} The first setCookie function is for testing purposes to see if the if block has an effect on it. None of these work. When I check in the Application.Cookies tab i see neither a "userIsAdmin" cookie or "name" cookie.setCookie is imported from the nookies npm package.This is the full authorize function:
async authorize(credentials): Promise<UserCustom | null> {
if (!credentials?.username || !credentials?.password) return null;
const { username, password } = credentials;
try {
const res = await fetch(
`${process.env.FOODBASKET_AUTH_API_URL}/pos-user/login`,
{
method: "POST",
body: JSON.stringify({ username, password }),
headers: {
"Content-Type": "application/json",
},
}
);
if (!res.ok) {
const error = await res.json();
throw new Error(error.message);
}
const user = await res.json();
setCookie({ res }, "name", "value", {
maxAge: 2 * 24 * 60 * 60,
path: "/",
httpOnly: true,
});
if (user.user.isAdmin) {
console.log("set cookie");
setCookie({ res }, "userIsAdmin", "1", {
maxAge: 60 * 60,
httpOnly: true,
path: "/",
});
}
return user;
} catch (error: any) {
console.log({ error });
throw new Error(error.message);
}
},