Skip to content

fix(auth): implement idle-session activity heartbeat (#583) - #748

Merged
MaryammAli merged 1 commit into
BlockDash-Studios:mainfrom
azahjessica49-commits:fix/ba-015-idle-session-heartbeat
Aug 29, 2026
Merged

fix(auth): implement idle-session activity heartbeat (#583)#748
MaryammAli merged 1 commit into
BlockDash-Studios:mainfrom
azahjessica49-commits:fix/ba-015-idle-session-heartbeat

Conversation

@azahjessica49-commits

Copy link
Copy Markdown
Contributor

Overview

This PR implements an idle-session activity heartbeat mechanism so that authenticated activity updates a session's lastActivityAt timestamp, making the configured idle timeout actually useful.

Related Issue

Closes #583

Changes

[ADD] Session activity tracking

  • lastActivityAt: Date added to Session interface and populated on creation
  • validateSession() — validates expiry/idle and atomically advances lastActivityAt
  • validateAndRefreshSession() — same, exposed for tutor-guard usage
  • updateLastActivity() — explicit heartbeat updater used by guards and new endpoint
  • Per-session withRefreshLock — prevents concurrent heartbeat updates from regressing timestamps

[ADD] Heartbeat API endpoint

  • POST /auth/session/heartbeat — allows clients to ping and keep an idle session alive

[FIX] Corrupted auth-session.service.ts

  • Removed duplicate methods, stray tokens, and broken Redis API calls
  • Fixed refresh-token reuse to correctly revoke all user sessions
  • Populated absoluteExpiresAt, idleExpiresAt, deliveryGraceSeconds, and lastActivityAt on session creation

[FIX] JWT guards

  • JwtLearnerGuard — fixed completely broken imports (@nestj/common, JstService, SessionService)
  • JwtTutorGuard — fixed this&jwtService and UserRole.TUTIOR typos
  • JwtPayload — added sessionId and type so guards can validate sessions from access tokens

Verification Results

npm test -- src/auth/auth-session.service.spec.ts
7/7 passed

npm test -- src/auth/jwt-clock-skew.spec.ts
10/10 passed

npx tsc --noEmit
No auth-related TypeScript errors

Acceptance Criteria

Criterion Status
Valid activity updates lastActivityAt validateSession() advances lastActivityAt and idleExpiresAt atomically
Inactive sessions expire isSessionIdle() uses lastActivityAt; idle sessions are revoked on validation/refresh
Concurrent heartbeat updates do not regress timestamps ✅ Per-session withRefreshLock serializes writes; monotonic check newActivity > previousActivity

…os#583)

- Add lastActivityAt to Session interface and populate it on creation
- Implement validateSession() to check expiry/idle and advance lastActivityAt
- Implement validateAndRefreshSession() as activity-aware guard helper
- Add POST /auth/session/heartbeat endpoint for explicit client pings
- Use per-session refresh lock to prevent concurrent heartbeat regressions
- Fix corrupted auth-session.service.ts (duplicate methods, syntax errors)
- Fix JwtLearnerGuard broken imports (SessionService, typos)
- Fix JwtTutorGuard typos (jwtService, TUTOR)
- Update JwtPayload to include sessionId/type for guard validation
- Update auth-session spec and jwt-clock-skew spec for new signatures
- Ensure refresh-token reuse revokes all user sessions
@drips-wave

drips-wave Bot commented Aug 29, 2026

Copy link
Copy Markdown

@azahjessica49-commits Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@MaryammAli MaryammAli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@MaryammAli
MaryammAli merged commit 178a3c9 into BlockDash-Studios:main Aug 29, 2026
1 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BA-015 — Implement idle-session activity updates

2 participants