Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/betterAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { triggerTypedEvent } from './common/typedPubsub';
import { sendEmail, CioTransactionalMessageTemplateId } from './common/mailing';
import { handleRegex } from './common/object';
import { validateAndTransformHandle } from './common/handles';
import { ONE_DAY_IN_SECONDS, ONE_MONTH_IN_SECONDS } from './common/constants';
import { ONE_MONTH_IN_SECONDS, ONE_HOUR_IN_SECONDS } from './common/constants';
import { singleRedisClient } from './redis';
import { User } from './entity/user/User';
import { cookies, extractRootDomain } from './cookies';
Expand Down Expand Up @@ -399,7 +399,7 @@ export const getBetterAuthOptions = (pool: Pool): BetterAuthOptions => {
modelName: 'ba_session',
storeSessionInDatabase: true,
expiresIn: ONE_MONTH_IN_SECONDS,
updateAge: ONE_DAY_IN_SECONDS,
updateAge: 12 * ONE_HOUR_IN_SECONDS,
},
account: {
modelName: 'ba_account',
Expand Down
2 changes: 1 addition & 1 deletion src/cron/cleanExpiredBetterAuthSessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const cleanExpiredBetterAuthSessions: Cron = {
name: 'clean-expired-better-auth-sessions',
handler: async (con, logger) => {
const result = await con.query(
`DELETE FROM ba_session WHERE "expiresAt" < NOW()`,
`DELETE FROM ba_session WHERE "expiresAt" < NOW() - INTERVAL '1 hour'`,
);

const deleted = Array.isArray(result) ? result[1] : (result?.affected ?? 0);
Expand Down
24 changes: 17 additions & 7 deletions src/routes/boot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import {
} from '../common/profile/completion';
import { getUnreadNotificationsCount } from '../notifications/common';
import { unwrapArray } from '../common/array';
import { asyncRetry } from '../integrations/retry';

export type BootSquadSource = Omit<GQLSource, 'currentMember'> & {
permalink: string;
Expand Down Expand Up @@ -925,12 +926,17 @@ export const getBootData = async (

const baSessionCookie = req.cookies[cookies.authSession.key];
if (baSessionCookie) {
let sessionInvalid = false;
try {
const session = (await getBetterAuth().api.getSession({
headers: fromNodeHeaders(
req.headers as Record<string, string | string[] | undefined>,
),
})) as BetterAuthSession | null;
const session = (await asyncRetry(
() =>
getBetterAuth().api.getSession({
headers: fromNodeHeaders(
req.headers as Record<string, string | string[] | undefined>,
),
}),
{ retries: 1, minTimeout: 50 },
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why only one retry though?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do more but 2 tries in total seemed ok to catch edge-cases?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

retries 1 means we will try only once and then stop, doesn't it?
it doesn't mean we will try once and then another time on failure. but maybe i'm wrong

Comment thread
rebelchris marked this conversation as resolved.
Outdated
)) as BetterAuthSession | null;

if (session) {
req.userId = session.user.id;
Expand All @@ -947,14 +953,18 @@ export const getBootData = async (
}

req.log.warn('BetterAuth getSession returned null');
sessionInvalid = true;
} catch (error) {
req.log.error(
{ err: error instanceof Error ? error.message : String(error) },
'BetterAuth session validation failed',
);
}
req.log.warn('BetterAuth session cookie present but validation failed');
setCookie(req, res, 'authSession', undefined);

if (sessionInvalid) {
req.log.warn('BetterAuth session cookie present but session invalid');
setCookie(req, res, 'authSession', undefined);
}
}

if (req.userId && req.accessToken?.expiresIn) {
Expand Down
Loading