fix: show proper error message on outbound cookie cache data

This commit is contained in:
Bereket Engida
2024-12-12 15:08:25 +03:00
parent 26a79f5934
commit 2f299f8b93
5 changed files with 21 additions and 70 deletions

View File

@@ -17,7 +17,6 @@ import { resend } from "./email/resend";
import { MysqlDialect } from "kysely";
import { createPool } from "mysql2/promise";
import { nextCookies } from "better-auth/next-js";
import { addAccountToSession } from "./plugin";
const from = process.env.BETTER_AUTH_EMAIL || "delivered@resend.dev";
const to = process.env.TEST_EMAIL || "";
@@ -43,12 +42,6 @@ export const auth = betterAuth({
dialect,
type: process.env.USE_MYSQL ? "mysql" : "sqlite",
},
session: {
cookieCache: {
enabled: true,
maxAge: 60,
},
},
emailVerification: {
async sendVerificationEmail({ user, url }) {
console.log("Sending verification email to", user.email);
@@ -155,6 +148,5 @@ export const auth = betterAuth({
oneTap(),
oAuthProxy(),
nextCookies(),
addAccountToSession,
],
});

View File

@@ -1,40 +0,0 @@
import { BetterAuthPlugin } from "better-auth";
export const addAccountToSession = {
id: "add-account-to-session",
hooks: {
after: [
{
matcher(context) {
return context.path.startsWith("/callback");
},
async handler(ctx) {
const sessionCookie = ctx.responseHeader.get(
ctx.context.authCookies.sessionToken.name,
);
if (!sessionCookie) {
return;
}
const provider = ctx.path.split("/callback")[1];
if (!provider) {
return;
}
const sessionId = sessionCookie.split(".")[0];
await ctx.context.internalAdapter.updateSession(sessionId, {
accountId: provider,
});
},
},
],
},
schema: {
session: {
fields: {
accountId: {
type: "string",
required: false,
},
},
},
},
} satisfies BetterAuthPlugin;

View File

@@ -394,9 +394,6 @@ export const router = <C extends AuthContext, Option extends BetterAuthOptions>(
e.message.includes("does not exist")
) {
ctx.logger?.error(e.message);
ctx.logger?.error(
"If you are seeing this error, it is likely that you need to run the migrations for the database or you need to update your database schema. If you recently updated the package, make sure to run the migrations.",
);
return;
}
}

View File

@@ -281,7 +281,6 @@ export const sessionMiddleware = createAuthMiddleware(async (ctx) => {
if (!session?.session) {
throw new APIError("UNAUTHORIZED");
}
return {
session,
};

View File

@@ -127,13 +127,9 @@ export async function setSessionCookie(
}
const shouldStoreSessionDataInCookie =
ctx.context.options.session?.cookieCache?.enabled;
shouldStoreSessionDataInCookie &&
ctx.setCookie(
ctx.context.authCookies.sessionData.name,
JSON.stringify(
base64url.encode(
new TextEncoder().encode(
JSON.stringify({
if (shouldStoreSessionDataInCookie) {
const data = JSON.stringify({
session: session,
expiresAt: getDate(
ctx.context.authCookies.sessionData.options.maxAge || 60,
@@ -143,12 +139,19 @@ export async function setSessionCookie(
value: JSON.stringify(session),
secret: ctx.context.secret,
}),
}),
),
),
),
});
if (data.length > 4093) {
throw new BetterAuthError(
"Session data is too large to store in the cookie. Please disable session cookie caching or reduce the size of the session data",
);
}
ctx.setCookie(
ctx.context.authCookies.sessionData.name,
data,
ctx.context.authCookies.sessionData.options,
);
}
ctx.context.setNewSession(session);
/**
* If secondary storage is enabled, store the session data in the secondary storage