I have a server action that with an api request. I'm using NextAuth for authentication. I would like to sign the user out or invalidate their session if this API response status returns a 401. This code is currently not working. How can I make it work? I'm using the app router.
export async function getUsers(page: number) {
const session = await getServerSession(authOptions);
try {
const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/admin/users?skip=${page * 10}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
"api-key": process.env.API_KEY!,
"Authorization": `Bearer ${session.jwt}`
}
});
console.log(response.status)
if (response.status === 401) {
redirect('/api/auth/signout')
}
return await response.json();
} catch (err) {
console.log(err);
}
}