NextAuth cannot read sessionToken
Answered
Scaly-naped Pigeon posted this in #help-forum

Scaly-naped PigeonOP
I am trying to create a signin with google with next auth but I always receive cannot read sessionToken and actually when I debug I can see that google has sent me the response , i am using custom functions because i am using a separated backend and i cannot use an adapters
import api from "@/eden";
import { AuthOptions, DefaultSession } from "next-auth";
import GoogleProvider from "next-auth/providers/google";
import crypto from "crypto";
declare module "next-auth" {
interface Session extends DefaultSession {
user: User & DefaultSession["user"];
status: string;
sessionToken?: string;
}
interface User {
id: string;
email: string;
}
}
const authOptions: AuthOptions = {
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
}),
],
callbacks: {
async redirect({ baseUrl }) {
return baseUrl;
},
async session({ session, token }) {
if (session.user) {
session.user.id = token.id as string;
session.sessionToken = token.sessionToken as string;
}
return session;
},
async jwt({ token, user }) {
if (user) {
token.id = user.id;
token.sessionToken = crypto.randomUUID().toString();
}
return token;
},
},
session: {
strategy: "database",
maxAge: 6 * 30 * 24 * 60 * 60,
},
events: {
async signIn({ user }) {
const sessionToken = crypto.randomUUID().toString();
const expires = new Date(Date.now() + 6 * 30 * 24 * 60 * 60 * 1000);
await api.auth.session.post({
sessionToken,
userId: user.id,
expires: expires.toISOString(),
});
},
async signOut({ token }) {
const sessionToken = token?.sessionToken as string;
if (!sessionToken) {
return;
}
await api.auth.session.delete({ sessionToken });
},
},
};
export { authOptions };
Answered by Scaly-naped Pigeon
fixed the issue by changing strategy to jwt instead of database
1 Reply

Scaly-naped PigeonOP
fixed the issue by changing strategy to jwt instead of database
Answer