Hey I have a login issue with Descope + Next.js (A...
# ask-a-descoper
a
Hey I have a login issue with Descope + Next.js (App Router). When a user logs in (SSO or Magic Link), the flow redirects — but for ~1 second they briefly see the /login page again before reaching the dashboard. Tried both server and client components — get the same behavior. On that first render: • await session() is undefined (if using server component) • isSessionLoading / isUserLoading are false (so I can’t show a loader). (if using the client component) Is there a recommended way to handle this in App Router (middleware / callback) so the user doesn’t see the login page flash?
m
looping @Asaf Shen @Rahul Rijhwani here
r
Let me take a look
a
adding the code for both versions: server component:
Copy code
import { Descope } from "@descope/nextjs-sdk";
import { session } from "@descope/nextjs-sdk/server"; // eslint-disable-line
import { redirect } from "next/navigation";

export default async function LoginPage() {
  const sessionRes = await session();
  console.log("sessionRes", sessionRes);
  if (sessionRes) {
    redirect("/dashboard");
  }
  return (
    <>
      <p>{!sessionRes ? "User is not logged in and see this as flash messege" : "User is logged in"}</p>
      <Descope
        flowId={process.env.NEXT_PUBLIC_DESCOPE_FLOW_ID || "sign-in"}
        redirectAfterSuccess="/dashboard"
      />
    </>
  );
}
client component:
Copy code
"use client";

import { AppLoading } from "@/components/common";
import { Descope } from "@descope/nextjs-sdk";
import { useSession, useUser } from "@descope/nextjs-sdk/client";
import { useRouter } from "next/navigation";
import { useEffect, useState } from "react";

export default function LoginPage() {
  const { isAuthenticated, isSessionLoading } = useSession();
  const { isUserLoading } = useUser();
  const router = useRouter();
  const [mounted, setMounted] = useState(false);

  useEffect(() => {
    setMounted(true);
  }, []);

  useEffect(() => {
    if (isAuthenticated && !isSessionLoading && !isUserLoading) {
      router.replace("/dashboard");
    }
  }, [isAuthenticated, router, isSessionLoading, isUserLoading]);

  if (!mounted || isSessionLoading || isUserLoading) return <AppLoading />;

  return (
    <>
      <h1>Title that the user see as flash message</h1>
      <Descope
        flowId={process.env.NEXT_PUBLIC_DESCOPE_FLOW_ID || "sign-in"}
        onSuccess={() => {
          router.push("/dashboard");
        }}
        onError={(e: { detail: { message: string } }) => {
          console.error("Error:", e.detail.message);
        }}
        redirectAfterSuccess="/dashboard"
      />
    </>
  );
}
also happend when i import as no ssr component:
Copy code
"use client";

import { AppLoading } from "@/components/common";
import { useSession, useUser } from "@descope/nextjs-sdk/client";
import dynamic from "next/dynamic";
import { useRouter } from "next/navigation";
import { useEffect } from "react";

const Descope = dynamic(
  () => import("@descope/nextjs-sdk").then(mod => mod.Descope),
  { ssr: false }
);

export default function LoginPage() {
  const { isAuthenticated, isSessionLoading } = useSession();
  const { isUserLoading } = useUser();
  const router = useRouter();

  useEffect(() => {
    if (isAuthenticated && !isSessionLoading && !isUserLoading) {
      router.replace("/dashboard");
    }
  }, [isAuthenticated, router, isSessionLoading, isUserLoading]);

  if (isSessionLoading || isUserLoading) return <AppLoading />;

  return (
    <>
      <h1>Title that the user see as flash message</h1>
      <Descope
        flowId={process.env.NEXT_PUBLIC_DESCOPE_FLOW_ID || "sign-in"}
        onSuccess={() => {
          router.push("/dashboard");
        }}
        onError={(e: { detail: { message: string } }) => {
          console.error("Error:", e.detail.message);
        }}
        redirectAfterSuccess="/dashboard"
      />
    </>
  );
}
r
Hi @Adi Strauss We are checking this internally. Will get back to you soon.
a
no problem! if you need anything on my side let me know 🙂 Im available.. @Rahul Rijhwani
👍 1
@Rahul Rijhwani checking for updates
r
Hi @Adi Strauss, @Asaf Shen is working on it, He will share update soon once he will get any thing.
🙌 1
d
@Adi Strauss why arent you using nextjs middleware for the logic?
a
@Dafna Libis I do as well... but i want to make sure that if the user go to the login page and he is connected it will redirect him to the dashboard page
Copy code
import { authMiddleware } from "@descope/nextjs-sdk/server";

export default authMiddleware({
  projectId: process.env.NEXT_PUBLIC_DESCOPE_PROJECT_ID,
  redirectUrl: "/login",
  publicRoutes: ["/login", "/_next", "/favicon.ico"],
});

export const config = {
  matcher: ["/((?!.+\\.[\\w]+$|_next).*)", "/", "/(api|trpc)(.*)"],
};
d
@Adi Strauss I think you should do all this logic in the niddleware, then wha you're experiencing wont happen
Copy code
export async function middleware(request: NextRequest) {
 const res = await descopeMiddleware(request);
if (someLogic) {
return NextResponse.redirect(new URL('/dashboard', request.url));
}
return res
}
you can use
import { session } from '@descope/nextjs-sdk/server';
same as you did in the server component
a
@Dafna Libis Im not sure I follow.. do you suggest to have 2 middleware? one setting the authMiddleware with the project config and the other one for redirection? never seen this pattern in the docs..
d
@Adi Strauss one middleware,same one youve shown, its true, ita not in the docs, this pattern was suggested by @Asaf Shen after i talked with him
a
@Dafna Libis Thank you for the solution! it helped me to create a better loading state! Amazing! 🎉