Large number of Vercel Edge Middleware Invocations coming from next-auth's `/api/auth/session`

I am using the create-t3-app with NextAuth for auth. Also using the pages router for this project. I had about about 3k page views yesterday when I noticed that my Edge Middleware Invocations went up by 4% in one day, with /api/auth/session out in the lead with over a 1/4 of all invocations. I am using the database strategy with NextAuth
import { PrismaAdapter } from '@next-auth/prisma-adapter';
import { type User } from '@prisma/client';
import { type GetServerSidePropsContext } from 'next';
import { getServerSession, type DefaultSession, type NextAuthOptions } from 'next-auth';
import GoogleProvider from 'next-auth/providers/google';
import { env } from '../env.mjs';
import db from './db';

declare module 'next-auth' {
interface Session extends DefaultSession {
user: DefaultSession['user'] & User;
}
}

export const authOptions: NextAuthOptions = {
adapter: PrismaAdapter(db),
providers: [
GoogleProvider({
clientId: env.AUTH_GOOGLE_ID,
clientSecret: env.AUTH_GOOGLE_SECRET,
}),
],
callbacks: {
session: (s) => {
return { ...s.session, user: { ...s.session.user, ...s.user } };
},
},
events: {
linkAccount: async ({ user }) => {
await db.user.update({
where: { id: user.id },
data: { emailVerified: new Date() },
});
},
},
pages: {
signIn: '/auth/login',
error: '/auth/error',
newUser: '/auth/register',
},
};

export const getServerAuthSession = (ctx: { req: GetServerSidePropsContext['req']; res: GetServerSidePropsContext['res'] }) => {
return getServerSession(ctx.req, ctx.res, authOptions);
};
import { PrismaAdapter } from '@next-auth/prisma-adapter';
import { type User } from '@prisma/client';
import { type GetServerSidePropsContext } from 'next';
import { getServerSession, type DefaultSession, type NextAuthOptions } from 'next-auth';
import GoogleProvider from 'next-auth/providers/google';
import { env } from '../env.mjs';
import db from './db';

declare module 'next-auth' {
interface Session extends DefaultSession {
user: DefaultSession['user'] & User;
}
}

export const authOptions: NextAuthOptions = {
adapter: PrismaAdapter(db),
providers: [
GoogleProvider({
clientId: env.AUTH_GOOGLE_ID,
clientSecret: env.AUTH_GOOGLE_SECRET,
}),
],
callbacks: {
session: (s) => {
return { ...s.session, user: { ...s.session.user, ...s.user } };
},
},
events: {
linkAccount: async ({ user }) => {
await db.user.update({
where: { id: user.id },
data: { emailVerified: new Date() },
});
},
},
pages: {
signIn: '/auth/login',
error: '/auth/error',
newUser: '/auth/register',
},
};

export const getServerAuthSession = (ctx: { req: GetServerSidePropsContext['req']; res: GetServerSidePropsContext['res'] }) => {
return getServerSession(ctx.req, ctx.res, authOptions);
};
and i call getServerAuthSession in createTRPCContext
export const createTRPCContext = async ({ req, res }: CreateNextContextOptions) => {
const session = await getServerAuthSession({ req, res });

return createInnerTRPCContext({ session });
};
export const createTRPCContext = async ({ req, res }: CreateNextContextOptions) => {
const session = await getServerAuthSession({ req, res });

return createInnerTRPCContext({ session });
};
Is this to be expected, or am i doing something wrong?
No description
0 Replies
No replies yetBe the first to reply to this messageJoin