Next.js Discord

Discord Forum

SS redirect renders the page twice (strict mode off)

Unanswered
Polar bear posted this in #help-forum
Open in Discord
Polar bearOP
import { setCookie } from "@/lib/ssa/setCookie";
import { Valid } from "@/components/Valid";
import { ApiInstance } from "@/constants";
import { AuthActions } from "@/lib/AuthActions";
import { IUser } from "@/types";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
type IVerify = {
  status: "success" | "failed" | "verified";
  user: IUser | null;
  sid: string | null;
};
async function verifyEmail(id: string, token: string, c): Promise<IVerify> {
  try {
    let user: IUser | null = await AuthActions.fetchUser({
      headers: {
        Cookie: c.toString(),
      },
    });

    if (user && user.verified) {
      return { sid: null, status: "verified", user: null };
    }
    const response = await ApiInstance.get(
      `/auth/credentials/verify/${id}/${token}`
    );
    const rawCookie = response.headers["set-cookie"]?.find((str) =>
      str.startsWith("connect.sid")
    );
    const sid = rawCookie?.split(";")[0].split("=")[1] as string;

    user = await AuthActions.fetchUser({
      headers: { Cookie: rawCookie },
    });

    return { user, sid, status: "success" };
  } catch (error: any) {
    return { sid: null, user: null, status: "failed" };
  }
}

async function Verify({
  params: { id, token },
}: {
  params: { id: string; token: string };
}) {
  const c = cookies();
  const { status, sid, user } = await verifyEmail(id, token, c);
  if (status === "verified") redirect("/");
...
}

export default Verify;

0 Replies