NextAuth useSession() data always undefined and status always loading
Unanswered
Gouty oak gall posted this in #help-forum
Gouty oak gallOP
I saw this post: https://github.com/nextauthjs/next-auth/issues/2941
but I am using the correct version api
All of a sudden, my calls to
The only thing i did vs previously is that i added a callback in my
Here's the
the console.log on the username returns the actual username when logging in with the provider, so I assume something must be wrong with the session set up but im not sure exactly what
but I am using the correct version api
All of a sudden, my calls to
useSession() always return undefined for the data now and loading.. I call useSession() like so:import {useSession} from 'next-auth/react';
...
export default MyClientComponent() {
const { data: sessionData } = useSession()
if (
sessionData?.user?.email === undefined ||
sessionData?.user?.email === null
) {
return redirect('/');
}
}The only thing i did vs previously is that i added a callback in my
auth.ts file but even after going back to previous commits, the useSession() call no longer works anymore.. not sure what's wrongHere's the
auth.ts file callback I added:callbacks: {
async signIn({user, account, profile, email, credentials}) {
console.log('sign in');
const username = user.email;
if (!username) {
return false;
}
const userAccount = await getAccount(username);
console.log('username=', username);
console.log('userAccount=', userAccount);
if (userAccount.status !== 200 || userAccount.body === null) {
await createAccount(username);
console.log('account created: ', username);
}
return true;
},
},the console.log on the username returns the actual username when logging in with the provider, so I assume something must be wrong with the session set up but im not sure exactly what
1 Reply
Gouty oak gallOP
It seems that
I tried adding this
but still doesn't work as i intended since i want to redirect to login if a user hasn't logged in yet, so not really sure what the best way to approach this is
sessionData may be undefined/loading when the session hasn't been fetched yet?I tried adding this
useEffect call from this post: https://stackoverflow.com/questions/70149852/next-auth-usesession-returning-undefined-on-initial-page-loadbut still doesn't work as i intended since i want to redirect to login if a user hasn't logged in yet, so not really sure what the best way to approach this is