Skip to content

Open source personal finance application

feat(auth): enhance session management and cookie handling in middleware

Ilya Komichev089873ff67cd
diff --git a/app/middleware/auth.global.ts b/app/middleware/auth.global.ts
index e44e4219..4e7336a4 100644
--- a/app/middleware/auth.global.ts
+++ b/app/middleware/auth.global.ts
@@ -1,5 +1,5 @@
import { useDemo } from '~/components/demo/useDemo'
-import { hasAuthCookie, setAuthCookie, useAuthCookieSSR } from '~/composables/useAuthCookie'
+import { clearAuthCookie, hasAuthCookie, setAuthCookie, useAuthCookieSSR } from '~/composables/useAuthCookie'
import { createLogger } from '~/utils/logger'
const logger = createLogger('auth/middleware')
@@ -69,6 +69,14 @@ export default defineNuxtRouteMiddleware(async (to) => {
if (!sessionInitialized) {
sessionInitialized = true
authClient.getSession()
+ .then((session) => {
+ const user = session.data?.user || null
+ if (!user) {
+ logger.error('Background getSession: session expired, redirecting to login')
+ clearAuthCookie()
+ navigateTo('/login')
+ }
+ })
.catch((error) => {
logger.error('Background getSession failed:', error)
sessionInitialized = false
diff --git a/app/plugins/convex.ts b/app/plugins/convex.ts
index d8bc1dab..9c46bbce 100644
--- a/app/plugins/convex.ts
+++ b/app/plugins/convex.ts
@@ -2,7 +2,7 @@ import type { ConvexClient, ConvexHttpClient } from 'convex/browser'
import { createConvexClient, createConvexHttpClient } from '~~/services/convex/client'
-import { hasAuthCookie } from '~/composables/useAuthCookie'
+import { clearAuthCookie, hasAuthCookie } from '~/composables/useAuthCookie'
import { createLogger } from '~/utils/logger'
const logger = createLogger('convex')
@@ -34,7 +34,7 @@ export default defineNuxtPlugin(() => {
const fetchConvexToken = async (forceRefreshToken: boolean) => {
const controller = new AbortController()
- const timeout = setTimeout(() => controller.abort(), 5000)
+ const timeout = setTimeout(() => controller.abort(), 10000)
try {
// Use raw fetch with explicit cookie forwarding — cross-domain
@@ -125,6 +125,7 @@ export default defineNuxtPlugin(() => {
else {
client.client.clearAuth()
authSet = false
+ clearAuthCookie()
}
},
)
diff --git a/convex/auth.ts b/convex/auth.ts
index a3ed9cae..135e285e 100644
--- a/convex/auth.ts
+++ b/convex/auth.ts
@@ -45,6 +45,10 @@ function createAuth(ctx: GenericCtx<DataModel>) {
siteUrl: frontendUrl,
}),
],
+ session: {
+ expiresIn: 60 * 60 * 24 * 90, // 90 days
+ updateAge: 60 * 60 * 24, // 1 day (refresh on each visit)
+ },
socialProviders: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID!,