#How to update the session on login on server side?

23 messages · Page 1 of 1 (latest)

swift cape
#
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

candid breachBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

swift cape
#

Am I doing anything fundamentally wrong?

silk quiver
swift cape
#

that did fixed it

#

any other way of doing this?

#

or is this the standard

silk quiver
#

only router.refresh() is able to invalidate the router cache

#

on client side

swift cape
#

But the above component is a server component

silk quiver
#

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 ?

swift cape
#

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;
      },
    }),
silk quiver
#

yes but how do you call it?

swift cape
#

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?

silk quiver
#

you can try

#

not sure if it will invalidate the router cache

#

I think it does

swift cape
#

It did fix the issue but I was getting "uncaught in promise" error so moved to teh first optiion

silk quiver
#

please mark solution if your issue has been solved