With Next.js 14 app router structure, I am modifying my session inside […nextAuth].ts middleware with basic JWT and Session callbacks.
When I get session with useSession callback in client side it is working correctly however, because my layout is server side rendered thanks to app router; when I get session with getServerSession(), it returns me only name and email which is not modified version.
How to fix this?
[..nextAuth] route.ts
export const authOptions: NextAuthCustomOptions = {
debug: false,
pages: {
error: "/auth/error",
},
site: process.env.NEXTAUTH_URL,
providers: [
KeycloakProvider({
clientId: process.env.NEXT_PUBLIC_KEYCLOAK_ID || "",
clientSecret: process.env.NEXT_PUBLIC_KEYCLOAK_CLIENT_SECRET || "",
issuer: process.env.NEXT_PUBLIC_KEYCLOAK_ISSUER || "",
}),
],
secret: process.env.NEXTAUTH_SECRET,
callbacks: {
async session({ session, token }: ISessionCallbak) {
session.accessToken = token.accessToken;
session.expiresAt = token.expiresAt;
session.refreshTokenExpiresIn = token.refreshTokenExpiresIn;
session.refreshToken = token.refreshToken;
session.tokenType = token.tokenType;
session.registrationStep = token.registrationStep;
return session;
},
async jwt({ token, user, account, profile }: IJwtCallback) {
if (user) {
token.id = user.id;
}
...
layout.tsx
export default async function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
const session = await getServerSession(); //returns not modified session
I noticed that useSession (client side) session is modified successfully. But with getServerSession() it gives me default name and email data only.
Alperen Sözen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.