Session extension isn't happening with Chrome Extension SW + Fastify Setup

Love the simplicity of the setup. however, I am facing an issue with my current Chrome Extension (service worker) + Fastify & Prisma development. I am using the Email & Password authentication. the signin, signup and signout functionality are working perfectly. however, strange thing I noticed is that the client getSession() call for 'better-auth/client' isn't increasing the session expiry. so, basically if the session expiresIn is set to 120 and the updateAge is set to 30 , then no matter how many times the getSession() is called, the session expiry isn't extending, and becomes null after 120 seconds. in the network calls also I didn't observe anything except api/auth/get-session calls. I went through the entire documentation, but was unable to identify the fix and understand what exactly is considered as the "session is used" here : https://www.better-auth.com/docs/concepts/session-management#session-expiration
But whenever the session is used and the updateAge is reached, the session expiration is updated to the current time plus the expiresIn value.
any help with this will be greatly appreciated. thank you.
Session Management | Better Auth
Better Auth session management.
1 Reply
Abhi!
Abhi!OP3mo ago
fastify auth.js
import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { registration_template } from "./templates.js";
import { PrismaClient } from "@prisma/client";
import { sendEmail } from "../services/email_service.js";

const prisma = new PrismaClient();
export const auth = betterAuth({
trustedOrigins: ["chrome-extension://*"],
// better-auth needs database to manage the sessions and users
database: prismaAdapter(prisma, {
provider: "cockroachdb",
}),
// allow the signups and logins using email and password
emailAndPassword: {
enabled: true,
autoSignIn: false,
requireEmailVerification: true,
},
// testing configuration - 2 minute testing
session: {
expiresIn: 120, // 2 minutes total session life
updateAge: 30, // Extend session every 30 seconds of activity
cookieCache: {
enabled: true,
maxAge: 120, // Cookie expires in 2 minutes
},
},
advanced: {
useSecureCookies: false, // For localhost testing
generateId: () => crypto.randomUUID(),
},
// send the verification email
emailVerification: {
sendOnSignUp: true,
sendVerificationEmail: async ({ user, url, token }, request) => {
const redirection_url = `${url}verified_msg`;
console.log("Sending verification email to:", user.email);
console.log("Sending verification with url:", redirection_url);
const email_template = registration_template.replace("{{code}}", redirection_url);
await sendEmail(user.email, "TabBud | Verify your Email", email_template);
},
},
});
import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { registration_template } from "./templates.js";
import { PrismaClient } from "@prisma/client";
import { sendEmail } from "../services/email_service.js";

const prisma = new PrismaClient();
export const auth = betterAuth({
trustedOrigins: ["chrome-extension://*"],
// better-auth needs database to manage the sessions and users
database: prismaAdapter(prisma, {
provider: "cockroachdb",
}),
// allow the signups and logins using email and password
emailAndPassword: {
enabled: true,
autoSignIn: false,
requireEmailVerification: true,
},
// testing configuration - 2 minute testing
session: {
expiresIn: 120, // 2 minutes total session life
updateAge: 30, // Extend session every 30 seconds of activity
cookieCache: {
enabled: true,
maxAge: 120, // Cookie expires in 2 minutes
},
},
advanced: {
useSecureCookies: false, // For localhost testing
generateId: () => crypto.randomUUID(),
},
// send the verification email
emailVerification: {
sendOnSignUp: true,
sendVerificationEmail: async ({ user, url, token }, request) => {
const redirection_url = `${url}verified_msg`;
console.log("Sending verification email to:", user.email);
console.log("Sending verification with url:", redirection_url);
const email_template = registration_template.replace("{{code}}", redirection_url);
await sendEmail(user.email, "TabBud | Verify your Email", email_template);
},
},
});
chrome extension (plain js) auth-client.js
import { createAuthClient } from 'better-auth/client';
import { AUTH_CLIENT_ENDPOINT } from '../pages/Background/rest/endpoints';

const better_auth_client = createAuthClient({
baseURL: AUTH_CLIENT_ENDPOINT,
});

export const { signIn, signUp, getSession, signOut } = better_auth_client;
import { createAuthClient } from 'better-auth/client';
import { AUTH_CLIENT_ENDPOINT } from '../pages/Background/rest/endpoints';

const better_auth_client = createAuthClient({
baseURL: AUTH_CLIENT_ENDPOINT,
});

export const { signIn, signUp, getSession, signOut } = better_auth_client;

Did you find this page helpful?