mirror of
https://github.com/kikootwo/ReadMeABook.git
synced 2026-06-03 12:50:09 +00:00
Add session revocation & consolidate rate limiting
Add sessions_invalidated_at to users (migration + Prisma schema) to support immediate session revocation. Set sessionsInvalidatedAt when an admin revokes a user's login token and enforce revocation checks in auth middleware and the refresh endpoint (compare token iat against sessionsInvalidatedAt). Add optional iat fields to JWT payload types. Scrub token from browser history after token-login. Consolidate rate-limiting logic into src/lib/utils/rateLimit.ts (rename/merge previous auth/apiToken rate limiter implementations), remove the old apiTokenRateLimit.ts, and update imports and tests to use the new module.
This commit is contained in:
@@ -172,6 +172,7 @@ export async function requireAuth(
|
||||
select: {
|
||||
id: true,
|
||||
deletedAt: true,
|
||||
sessionsInvalidatedAt: true,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -186,6 +187,19 @@ export async function requireAuth(
|
||||
);
|
||||
}
|
||||
|
||||
// Check if session was invalidated after this token was issued
|
||||
if (user.sessionsInvalidatedAt && payload.iat &&
|
||||
payload.iat < Math.floor(user.sessionsInvalidatedAt.getTime() / 1000)) {
|
||||
logger.warn('Token issued before session invalidation', { userId: payload.sub });
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Unauthorized',
|
||||
message: 'Session has been revoked',
|
||||
},
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
// Add user to request
|
||||
const authenticatedRequest = request as AuthenticatedRequest;
|
||||
authenticatedRequest.user = {
|
||||
|
||||
Reference in New Issue
Block a user