I want to have a page (/login) where a state cookie will be set and after that the user should be redirected but it doesn't work. This is my code:
import { redirect, RedirectType } from "next/navigation";
import { cookies } from "next/headers";
import { COOKIE_NAME_PREFIX } from "@/constants/guidelines";
import { encode } from "@/lib/utils";
const generateRandomState = (): string => {
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
};
const generateAuthorizationURL = (state: string): string => {
const data = {
client_id: process.env.CLIENT_ID,
redirect_uri: `${process.env.NEXT_PUBLIC_APP_URL}/api/auth/callback`,
response_type: "code",
scope: ["identify", "guilds", "email"].join(" "),
state
};
return `https://discord.com/api/oauth2/authorize?${encode(data)}`;
};
export default function Page() {
const state = generateRandomState();
const authorizationURL = generateAuthorizationURL(state);
cookies().set(COOKIE_NAME_PREFIX + "state", state, {
secure: process.env.NODE_ENV === "production",
httpOnly: true,
sameSite: "lax",
path: "/"
});
return redirect(authorizationURL, RedirectType.push);
}