getServerSession() not returning session NextAuth
Unanswered
Horned oak gall posted this in #help-forum
Horned oak gallOP
I am trying to gain access to my user session in a server component but getServerSession() is returning an empty user object.
NextAuth config:
and the server component I am attempting to call getServerSession in:
NextAuth config:
import NextAuth from "next-auth"
import CredentialsProvider from "next-auth/providers/credentials"
import postUserLogin from "@/lib/postUserLogin";
export const handler = NextAuth({
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
email: {
label: "Email:",
type: "text",
placeholder: "example@gmail.com"
},
password: {
label: "Password:",
type: "password"
}
},
async authorize(credentials: { email?: string; password?: string}){
// This is where we will post the email and password to Hades... if login is succesful hades will return info and a JWT to use in session
// Docs: https://next-auth.js.org/configuration/providers/credentials
const user = await postUserLogin(credentials.email, credentials.password)
// TODO: LOGIN SUCCESSFUL, need to add response to session or jwt(objects returned are persisted to the JWT)
if (user) {
return user
} else {
return null
}
}
})
],
pages: {
signIn: '/auth/sign-in',
sign in
},
session: {
strategy: "jwt",
},
// secret: process.env.NEXTAUTH_SECRET,
// debug: process.env.NODE_ENV === "development",
callbacks: {
async jwt ({ token, user, session}) {
if(user){
token.backendToken = user.token;
token.userName = user.userName;
token.identityId = user.identityId;
token.userId = user.userId;
token.userRole = user.userRole;
}
return token;
},
async session ({ session, token, user}) {
if(token){
session.user.backendToken = token.backendToken
session.user.userName = token.userName;
session.user.identityId = token.identityId;
session.user.userId = token.userId;
session.user.userRole = token.userRole;
}
return session;
},
},
theme: {
colorScheme: "auto", // "auto" | "dark" | "light"
}
})
export { handler as GET, handler as POST }
and the server component I am attempting to call getServerSession in:
import {getServerSession} from "next-auth";
import { handler } from "@/app/api/auth/[...nextauth]/route";
export default async function ReservationsPage({params}: { params: { identityId: string, string } }) {
const session = await getServerSession(handler);
console.log(JSON.stringify(session))
return (
<>
</>
)
}
3 Replies
Horned oak gallOP
Bump
Horned oak gallOP
bump
Horned oak gallOP
So when calling getServerSession it doesn't seem to be making it to the callbacks...not 100% sure why going to start digging into the actual getServerSession() code provided in NextAuth to see if I can figure it out in there