Replies: 27 comments 16 replies
-
Hi, did u fix this problem? I get same and even worse. After Sign and redirect back, the status is unauthenticated, but the dev env works smooth. |
Beta Was this translation helpful? Give feedback.
-
I'm also having issues with the |
Beta Was this translation helpful? Give feedback.
-
I have the same issue. |
Beta Was this translation helpful? Give feedback.
-
I have the same problem... |
Beta Was this translation helpful? Give feedback.
-
I'm having the same issues. |
Beta Was this translation helpful? Give feedback.
-
I'm having the same issues. Exist a solution? |
Beta Was this translation helpful? Give feedback.
-
Hi,
Did you set up a session callback in your[ …nextauth.js] ?
…On Mon, Jan 16, 2023 at 7:32 PM Eduardo Fortuna ***@***.***> wrote:
I'm having the same issues.
Exist a solution?
—
Reply to this email directly, view it on GitHub
<#5719 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AO4HWVEH4ZDE2EKEEWNHGBTWSXSA7ANCNFSM6AAAAAASYAQSIQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
In my case when I was redirecting in the callback config I was getting this error. I changed my callback config to no longer redirect but instead to return true on successful authentication or return false. callbacks: {
async signIn({ account, profile }) {
const { email_verified } = profile as GoogleProfile;
if (account?.provider === 'google' && email_verified) {
return true;
}
return false;
},
}, On your signIn() click handler you can pass in a callbackUrl to redirect to a different page on your site. signIn('google', { callbackUrl: 'http://localhost:3000/dashboard' }) I would say in OP's issue they would need to use the signIn() method from next-auth and use the callbackUrl. |
Beta Was this translation helpful? Give feedback.
-
In my case it was a problem with the order of components in the entry Before <>
<SessionProvider session={session}>
<OptionalAuth>{children}</OptionalAuth>
</SessionProvider>
</> After <>
<OptionalAuth>
<SessionProvider session={session}>{children}</SessionProvider>
</OptionalAuth>
</> |
Beta Was this translation helpful? Give feedback.
-
Has anyone solved it? I'm having the same issue :( |
Beta Was this translation helpful? Give feedback.
-
same issue :( |
Beta Was this translation helpful? Give feedback.
-
I encountered an issue while attempting to use callbacks for signing in and returning a redirect route. However, I managed to resolve the problem by returning a boolean value. |
Beta Was this translation helpful? Give feedback.
-
I'm also currently experiencing this. Running NextJS in /api/auth/session endpoint returns logged in user. The data in Verbose logging on NextAuth doesn't show anything obvious to look at... |
Beta Was this translation helpful? Give feedback.
-
I fixed it I think. In my |
Beta Was this translation helpful? Give feedback.
-
It seems that after a successful login the /session api is not called. So I did something like the following in my LoginComponent
Edit:
So we use an internal library to execute the signin process on a different backend, I had to do this also on the signIn process, where I give in a callbackUrl and on successful login, I also execute a redirect via window.location.href to the success location and then the session gets correctly loaded. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
same here |
Beta Was this translation helpful? Give feedback.
-
I found a workaround by putting the session logic on SSR with another internal component on the client side containing the rest of the logic: Server: import { auth } from "@/auth";
export default async function Component() {
const session = await auth();
return <ComponentInner session={session} />;
} Client: export default function ComponentInner({ session }: { session: Session | null }) {
console.log(session?.user);
// logic
} |
Beta Was this translation helpful? Give feedback.
-
My workaround involves implementing a session provider that fetches "use client";
import React from "react";
import { Session } from "next-auth";
import { usePathname } from "next/navigation";
type SessionProviderProps = React.PropsWithChildren<{
session?: Session | null;
}>;
const sessionContext = React.createContext<Session | null>(null);
export function SessionProvider({ session: initialSession = null, children }: SessionProviderProps) {
const [session, setSession] = React.useState<Session | null>(initialSession);
const pathname = usePathname();
React.useEffect(() => {
const fetchSession = async () => {
if (!initialSession) {
const fetchedSession = await fetch("/api/auth/session").then((res) => res.json());
setSession(fetchedSession);
}
};
fetchSession();
}, [initialSession, pathname]);
return <sessionContext.Provider value={session}>{children}</sessionContext.Provider>;
}
export function useSession() {
return React.useContext(sessionContext);
} |
Beta Was this translation helpful? Give feedback.
-
Having the same issue, no matter what, when I use router.push or router.replace or even the callback from signIn, to redirect to any page after login, it doesn't pick up the new session from useSession until I do a manual reload of the page. window.location.href = path; to force redirect reload and it works fine for now. |
Beta Was this translation helpful? Give feedback.
-
I fixed this issue by creating a middleware.ts file in the root of my directory (at the same level as the app/ directory). My application doesn't let users enter the site until you login so my middleware setup was super simple. This is all of the code that I have export { default } from "next-auth/middleware"
export const config = {
matcher: ['/'], // Apply middleware to all routes or specify specific routes
}; You can modify what routes call the middleware before they are rendered by using the matcher. You can find more information here. Hopefully this helps! |
Beta Was this translation helpful? Give feedback.
-
I run into the same issue and after intensive investigation, I found out, the direct approach we followed may not have worked due to several reasons related to how client-side and server-side data fetching and hydration work in Next.js, especially with the SessionProvider from next-auth/react. Here are some potential reasons why it didn't work: Server-Side vs. Client-Side Context
and so many cases .... The fixIn the layout, I removed session from the
Client-Side Session fetching fixed the Issue for me, because, by fetching the session directly on the client side using |
Beta Was this translation helpful? Give feedback.
-
I encountered a similar issue where the useSession() hook status is "unauthenticated", but the next-auth.session-token is still present. The solution I implemented tries to refetch the session once to handle this edge case. Here's the approach:
Explanation: |
Beta Was this translation helpful? Give feedback.
-
updating the session after signing in will fix the problem |
Beta Was this translation helpful? Give feedback.
-
Issue DescriptionWhen conditionally rendering the Environment
What happenedThe application was showing users as unauthenticated even though:
Root CauseThe issue was caused by conditionally rendering the // Problem code in layout.jsx
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
{someCondition ? (
<SessionProvider>
{children}
</SessionProvider>
) : (
children
)}
</body>
</html>
);
} SolutionAlways render the // Fixed code in layout.jsx
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<SessionProvider>
{/* Any conditional logic can go here */}
{children}
</SessionProvider>
</body>
</html>
);
} Additional Notes
This bug can be confusing to debug since the cookie is present but the session appears invalid. Always ensure authentication providers wrap your application consistently. |
Beta Was this translation helpful? Give feedback.
-
I was using useSession() but it didn't worked for me. However, this worked for me, Use the below custom hook. 📁hooks/use-current-user.ts 'use client';
import { useEffect, useState } from 'react';
import { getSession } from 'next-auth/react';
import type { Session } from 'next-auth';
export function useCurrentUser() {
const [user, setUser] = useState<Session['user'] | null>(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
getSession().then((session) => {
setUser(session?.user ?? null);
setLoading(false);
});
}, []);
const isAuthenticated = !!user;
return { user, loading, isAuthenticated };
} ✅ This makes useCurrentUser() independent of useSession() and reliable on page load. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Question 💬
Hello i setup next auth with CredentialsProvider
in local work perfectfully but when i deploy it to windows server the issues has found
when i login -> logout -> login the value of status from useSession() is unauthenticated
but the token next-auth.session-token already exists, you can see my image below
this is status from useSession()

this is the cookies

How to reproduce ☕️
This is my [...nextauth].js
and this is my login function
Contributing 🙌🏽
No, I am afraid I cannot help regarding this
Beta Was this translation helpful? Give feedback.
All reactions