Getting no session from useSession() with NextAuth

I'm trying to implement a simple "username, password"-CredentialsProvider in my app. I'm using the standard prisma schema from create-t3-app, and just added a "password" prop to the User model. Im am using the authorize function in the authOptions of NextAuth ([...nextauth].ts) to check if a user with that username exists in my db, and also check if the password matches. If that's true, I create a new session like
// lifespan of session: 1 day
const expires = new Date();
expires.setDate(expires.getDate() + 1);

await prisma.session.create({
data: {
userId: user.id,
expires,
sessionToken: await hash(`${Math.random()}`),
},
});
// lifespan of session: 1 day
const expires = new Date();
expires.setDate(expires.getDate() + 1);

await prisma.session.create({
data: {
userId: user.id,
expires,
sessionToken: await hash(`${Math.random()}`),
},
});
and return the user object afterwards. In my custom login page I trigger NextAuth's signIn function like:
signIn("credentials", {
redirect: false,
username,
password,
});
signIn("credentials", {
redirect: false,
username,
password,
});
If given correct credentials, it's resolved without any errors, and a new session gets created in my db. Still when I check for session data using useSession() I always end up getting null. I am also missing the session cookie. I only have next-auth.csrf-token and next-auth.callback-url. Yes, I am providing the session using SessionProvider in my pages/_app.tsx. Why is the useSession hook not getting my data? What am I missing? My head is smoking...
12 Replies
Froxx
Froxx2y ago
Or maybe a better question would be: How does the session data get into the useSession hook in the first place? What action of NextAuth does that? Sorry if I'm missing the obvious, but I'm trying to get this thing work for 3 days now, and I can't manage to find out how the CredentialsProvider needs to feed data to the useSession hook
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Froxx
Froxx2y ago
1. Yes, I use argon2. 2. I added a user manually to my db for now, yes. 3. That's exactly what I do. Still the data does not get returned by useSession. 4. I have an app with an internal user management. No public registration. I need to create user accounts manually from an admin account. So credentials seem to be the most forward way. Do you think different? And what would be your recommendation? No. What exactly does that mean? I mean, I know what a JWT is, but how do I need to define things in the context of NextAuth, and how does it affect the useSession hook to not receive the data
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Froxx
Froxx2y ago
My problem is not to figure out if my users' login attempts are successful or not. The problem is not having any session data in the hook after having logged in
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Froxx
Froxx2y ago
Although I would love to see it working, I'm a little relieved you didn't just 5-minute that thing for my own sanity 😄 Thanks a lot though for your effort already. I'll look forward for it 🙏
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Froxx
Froxx2y ago
Jesus Christ, I found my error. Since I started off with the default boilerplate delivered by ct3a I had a session callback in my [...nextauth].ts looking like this:
callbacks: {
session({ session, user }) {
if (session.user) {
session.user.id = user.id;
}
return session;
},
},
callbacks: {
session({ session, user }) {
if (session.user) {
session.user.id = user.id;
}
return session;
},
},
This probably works fine using the DiscordProvider used in the boilerplate, but since I removed that one and added my custom CredentialsProvider, my user object was undefined resulting in an error trying to call user.id resulting in no session object being returned at all. I even had that error shown quite obviously in my server's log. Don't ask me why I didn't see that. Probably a combination of operational blindness and frustration. Anyway, removing that callback resulted in a session object being set up correctly and returned by useSession after all. Thanks for the help guys 🙏
nimeshvaghasiya
@create-t3-maintainer @Froxx removing session callback(as per above comment) and set session strategy: 'jwt' is working for both credential provider and discord provider. what is the reason behind this? make below change to session callback does works for me: session({ session, user }) { if (session.user **&& user**) { session.user.id = user.id; } return session; },
asheeshh
asheeshh2y ago
there was no reason to ping every maintainer
nimeshvaghasiya
@asheesh my bad!
Want results from more Discord servers?
Add your server