Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 .infra/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ const redis = new Redis(`${name}-redis`, {
isAdhocEnv,
name: `${name}-redis`,
tier: 'BASIC',
memorySizeGb: 1,
memorySizeGb: 2,
region: location,
authEnabled: true,
redisVersion: 'REDIS_7_2',
labels: { app: name },
redisConfigs: {
'maxmemory-policy': 'volatile-ttl',
'maxmemory-gb': '0.95',
'maxmemory-gb': '1.9',
},
maintenancePolicy: {
weeklyMaintenanceWindows: [
Expand Down
31 changes: 31 additions & 0 deletions __tests__/boot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,37 @@ describe('logged in boot', () => {
});
});

it('should boot logged in user and refresh jwt when token expires within 3 minutes', async () => {
const accessToken = await signJwt(
{
userId: '1',
roles: [],
},
2 * 60 * 1000,
);
const key = app.signCookie(accessToken.token);
const res = await request(app.server)
.get(BASE_PATH)
.set('User-Agent', TEST_UA)
.set('Cookie', `${cookies.auth.key}=${key};`)
.expect(200);

expect(res.body).toEqual({
...LOGGED_IN_BODY,
user: {
...LOGGED_IN_BODY.user,
canSubmitArticle:
LOGGED_IN_BODY.user.reputation >= submitArticleThreshold,
},
});

const authCookie = setCookieParser.parse(res, { map: true })[
cookies.auth.key
];
expect(authCookie?.value).toBeTruthy();
expect(authCookie?.value).not.toEqual(key);
});

it('should not re-issue JWT token when isPlus in payload is same as user', async () => {
await saveFixtures(con, User, [
{
Expand Down
6 changes: 6 additions & 0 deletions __tests__/routes/betterAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ describe('betterAuth routes', () => {
expect(options.account).toMatchObject({
modelName: 'ba_account',
});
expect(options.session).toMatchObject({
modelName: 'ba_session',
storeSessionInDatabase: true,
expiresIn: 30 * 24 * 60 * 60,
updateAge: 24 * 60 * 60,
});
});

it('should forward native callback routes to BetterAuth handler', async () => {
Expand Down
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 } from './common/constants';
import { ONE_DAY_IN_SECONDS, ONE_MONTH_IN_SECONDS } from './common/constants';
import { singleRedisClient } from './redis';
import { User } from './entity/user/User';
import { cookies, extractRootDomain } from './cookies';
Expand Down Expand Up @@ -398,7 +398,7 @@ export const getBetterAuthOptions = (pool: Pool): BetterAuthOptions => {
session: {
modelName: 'ba_session',
storeSessionInDatabase: true,
expiresIn: 7 * ONE_DAY_IN_SECONDS,
expiresIn: ONE_MONTH_IN_SECONDS,
updateAge: ONE_DAY_IN_SECONDS,
},
account: {
Expand Down
3 changes: 2 additions & 1 deletion src/cookies.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CookieSerializeOptions } from '@fastify/cookie';
import { FastifyReply, FastifyRequest } from 'fastify';
import { ONE_MONTH_IN_SECONDS } from './common/constants';
import { generateTrackingId } from './ids';
import { setTrackingId } from './tracking';
import { counters } from './telemetry';
Expand Down Expand Up @@ -62,7 +63,7 @@ export const cookies: {
authSession: {
key: env === 'production' ? '__Secure-dast' : 'dast',
opts: {
maxAge: 60 * 60 * 24 * 7,
maxAge: ONE_MONTH_IN_SECONDS,
signed: false,
httpOnly: true,
secure: env === 'production',
Expand Down
16 changes: 6 additions & 10 deletions src/routes/boot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,9 @@ export const getBootData = async (
middleware?: BootMiddleware,
): Promise<AnonymousBoot | LoggedInBoot> => {
const referrer = getBootReferrer(req);
const shouldRefreshJwt =
!req.accessToken?.expiresIn ||
differenceInMinutes(req.accessToken.expiresIn, new Date()) <= 3;

const baSessionCookie = req.cookies[cookies.authSession.key];
if (baSessionCookie) {
Expand All @@ -933,14 +936,11 @@ export const getBootData = async (
req.userId = session.user.id;
req.trackingId = req.userId;
setTrackingId(req, res, req.trackingId);
const jwtValid =
req.accessToken?.expiresIn &&
differenceInMinutes(req.accessToken.expiresIn, new Date()) > 3;
return loggedInBoot({
con,
req,
res,
refreshToken: !jwtValid,
refreshToken: shouldRefreshJwt,
middleware,
userId: req.userId,
});
Expand All @@ -957,16 +957,12 @@ export const getBootData = async (
setCookie(req, res, 'authSession', undefined);
}

if (
req.userId &&
req.accessToken?.expiresIn &&
differenceInMinutes(req.accessToken?.expiresIn, new Date()) > 3
) {
if (req.userId && req.accessToken?.expiresIn) {
return loggedInBoot({
con,
req,
res,
refreshToken: false,
refreshToken: shouldRefreshJwt,
middleware,
userId: req.userId,
});
Expand Down
Loading