Next.js Discord

Discord Forum

How to update the session on login on server side?

Answered
Rex posted this in #help-forum
Open in Discord
RexOP
import Footer from "./components/footer/footer";
import Header from "./components/header/header";
import HomeHeader from "./components/header/homeHeader";
import { AuthProvider } from "../context/AuthProvider";
import { getCurrentUser } from "@/lib/services/getCurrentUser";
import { getServerSession } from "next-auth";
import { authOptions } from "./api/auth/[...nextauth]/route";
import { redirect } from "next/navigation";

export default async function RootLayout({ children }) {
  let user = "";
  // const user = await getCurrentUser();
  const session = await getServerSession(authOptions);

  if (session || session?.user) {
    user = session.user;
  }
  console.log("session lmao:", session);
  return (
    <html lang="en" className="overflow-x-hidden">
      <body>
        <AuthProvider session={session}>
          {user ? <Header /> : <HomeHeader />}
          {children}
          <Footer />
        </AuthProvider>
      </body>
    </html>
  );
}

The header doesn't updates when the user logins in. I am using next-auth withe next js
Answered by Ray
try calling router.refresh() after the login function
View full answer

21 Replies

RexOP
Am I doing anything fundamentally wrong?
@Rex Am I doing anything fundamentally wrong?
try calling router.refresh() after the login function
Answer
RexOP
that did fixed it
any other way of doing this?
or is this the standard
only router.refresh() is able to invalidate the router cache
on client side
RexOP
But the above component is a server component
I think this is the reason why next-auth v5 is moving the signin function to server only
then you can try to use revalidatePath
but i think you call the signin function from next-auth/react ?
RexOP
This is the authorize function in the auth route
    CredentialsProvider({
      name: "Credentials",
      credentials: {
        email: { label: "email", type: "text" },
        password: { label: "password", type: "password" },
      },
      async authorize(credentials) {
        if (!credentials?.email || !credentials.password) {
          throw new Error("invalid cred");
        }
        const user = await prisma.user.findUnique({
          where: {
            email: credentials.email,
          },
        });

        if (!user || !user?.hashedPassword) {
          throw new Error("Invalid Cred");
        }

        const isCorrectPassword = await bcrypt.compare(
          credentials.password,
          user.hashedPassword
        );
        if (!isCorrectPassword) {
          throw new Error("Invalid cred");
        }
        console.log("USER IN THE authorize", user);
        return user;
      },
    }),
yes but how do you call it?
RexOP
in login form function
  const onSubmit = async (data) => {
    setLoading(true);
    const { email, password } = data;
    const response = await signIn("credentials", {
      email,
      password,
      redirect: false,
    });
should i make the redirect true?
you can try
not sure if it will invalidate the router cache
I think it does
RexOP
It did fix the issue but I was getting "uncaught in promise" error so moved to teh first optiion
please mark solution if your issue has been solved