Next.js Discord

Discord Forum

next auth

Unanswered
Sloth bear posted this in #help-forum
Open in Discord
Sloth bearOP
when i use render.push('/') after logging in successfully, the session doesn't get update. how do i make it happen?
// server side
// getServerSession is from next auth
  const session = await getServerSession(options);

  if (session) {
    return <HomePage />;
  }

33 Replies

@ncls. Can you share your NextAuth config file, please?
Sloth bearOP
is this the config ?
const options: NextAuthOptions = {
  providers: [
    CredentialsProvider({
      name: 'Credentials',
      credentials: {
        email: { label: 'Email', type: 'text' },
        password: { label: 'Password', type: 'password' },
      },

      async authorize(credentials) {
        if (!credentials?.email || !credentials?.password) {
          return null;
        }

        const { email, password } = credentials;

        const user = await prisma.account.findUnique({
          where: { email },
        });

        if (user && (await bcrypt.compare(password, user.password))) {
          return user;
        } else {
          return null;
        }
      },
    }),
  ],

  pages: {
    signIn: '/account/login',
  },
};
Yes
You are using credentials
Meaning you are using JWT and no sessions
Sloth bearOP
i use credentials for my custom login page
@ncls. Meaning you are using JWT and no sessions
Sloth bearOP
idk, sessions do work, but it doesn't get refreshed when router.push. i have to reload the page manually in order for the logged in page to show
basically i have a landing page in the root route, and i want the landing page to get replaced when the user is logged
@ncls. Check this guide: https://next-auth.js.org/tutorials/securing-pages-and-api-routes#server-side
Sloth bearOP
unfortunately getServerSideProps doesn't work with the new app/ directory
i'm not sure what i'm missing, i'm really new to nextjs
@ncls. Can you share the full code for this?
Sloth bearOP
import Link from 'next/link';
import MainNavbar from '@/app/components/MainNavbar';
import { getServerSession } from 'next-auth';
import options from './api/auth/[...nextauth]/options';
import HomePage from './pages/HomePage';

const page = async () => {
  const session = await getServerSession(options);

  if (session) {
    return <HomePage />;
  }

  return (
    <>
      <MainNavbar />

      <div className="container mx-auto px-3 max-w-screen-xl">
      </div>
    </>
  );
};

export default page;
Sloth bearOP
the HomePage will show after you are authenticated
Yeah, but you said that it didn't work
Sloth bearOP
yes, i have to reload the page manually in order it to show
router.push still returns the landing page even authenticated
i think the page doesn't get fully reload when you router.push('/')
router.push() performs a soft navigation, meaning it does not reload the window but instead gets the new content from the server to update it dynamically. This makes the page faster and keeps traffic low
Sloth bearOP
yes, that's the reason why const session doesn't get updated
i tried console.log before if the statement but the console.log doesn't print anything
In which terminal? Server or browser?
Sloth bearOP
server side
i don't use use client
@Sloth bear i don't use `use client`
I mean what console did you check?
Sloth bearOP
i tried doing this, to confirm the page reloads when router.push and i'm correct it doesn't print anything

  console.log(session);
  if (session) {
    return <HomePage />;
  }
i use router.push when the user is logged in
What console did you check?
Sloth bearOP
ofc the server terminal
It should print something. At least undefined, null etc.
Sloth bearOP
it doesn't print anything when router.push
ofc the page is on landing page, and user click log in, then user logged in, it redirects back to the root route