My next auth session is always null.
Unanswered
Rohu posted this in #help-forum
RohuOP
Unsure if I misconfiged something but my session is always null when I access it serverSide with auth().
auth.ts
route.ts
accessing the session it being null:
auth.ts
import NextAuth from "next-auth";
import Credentials from "next-auth/providers/credentials";
import { verifyPassword } from "@/helpers/password";
import prisma from "./prisma";
import { PrismaAdapter } from "@auth/prisma-adapter";
export const { handlers, signIn, signOut, auth } = NextAuth({
// adapter: PrismaAdapter(prisma), Needed?
providers: [
//@ts-ignore
Credentials({
credentials: {
email: {},
password: {},
},
async authorize(credentials) {
const user = await prisma.user.findUnique({
where: {
email: credentials.email as string,
},
});
if (!user) {
await verifyPassword("password", credentials.password as string);
throw new Error("User not found.");
} else {
const isValidPassword = await verifyPassword(
credentials.password as string,
user.password_hash
);
if (!isValidPassword) {
throw new Error("User not found.");
}
const {password_hash, ...userWithOutPasswordHash} = user;
return userWithOutPasswordHash;
}
},
}),
],
session: {
strategy: "jwt",
},
callbacks: {
session: async ({ session, token }) => {
if (session.user) {
session.user.id = token.sub as string;
}
return session;
},
jwt: async ({ token }) => {
return token;
},
},
});route.ts
import { handlers } from "@/lib/auth" // Referring to the auth.ts we just created
export const { GET, POST } = handlersaccessing the session it being null:
"use server";
import { auth } from "@/lib/auth";
export async function getCart(ownerId: string) {
const sesh = await auth();
console.log(sesh)
}8 Replies
RohuOP
By the way, my api returns a normal user object so thats not the issue
"next-auth": "5.0.0-beta.22",
Vespid wasp
I had a similar problem some time ago.
When you use email and password for login with a custom database you need to set the session strategy to jwt and add a callback for it in your next auth config.
I sadly cannot find the article or documentation where I learned about it, but maybe part of my code helps you
“””
session: {
strategy: "jwt",
},
callbacks: {
jwt({ token, user }) {
if (user) {
// User is available during sign-in
token.id = user.id;
}
return token;
},
},
“””
When you use email and password for login with a custom database you need to set the session strategy to jwt and add a callback for it in your next auth config.
I sadly cannot find the article or documentation where I learned about it, but maybe part of my code helps you
“””
session: {
strategy: "jwt",
},
callbacks: {
jwt({ token, user }) {
if (user) {
// User is available during sign-in
token.id = user.id;
}
return token;
},
},
“””
RohuOP
@Vespid wasp isn't that exactly what I do?
Vespid wasp
This is true. Sorry, I overread the last lines.
For getting the session I use const { session } = await getUserAuth();
Instead of the auth function you use.
Maybe this helps? 😊
For getting the session I use const { session } = await getUserAuth();
Instead of the auth function you use.
Maybe this helps? 😊
RohuOP
getUserAuth where is that coming from 😄
Vespid wasp
Sorry, I forgot to sent most of my solution 😅
import { getServerSession } from "next-auth";
export const getUserAuth = async () => {
const session = await getServerSession(authOptions);
return { session } as AuthSession;
};
import { getServerSession } from "next-auth";
export const getUserAuth = async () => {
const session = await getServerSession(authOptions);
return { session } as AuthSession;
};
RohuOP
I think they way I grab the session doesn't matter the issue is that the session is null.