Invalidate auth sessions after password changes

This commit is contained in:
Francis Cao committed 2026-06-22 23:31:12 -07:00
1 parent 2e74b3ebae
commit 727490269b
4 files changed
+135 -7

No files matched your search

+6 -3
View File
@@ -1,7 +1,7 @@
import { z } from 'zod';
import { saveAuth } from '@/lib/auth';
import { ROLES } from '@/lib/constants';
import { secret } from '@/lib/crypto';
import { hash, secret } from '@/lib/crypto';
import { createSecureToken } from '@/lib/jwt';
import { checkPassword } from '@/lib/password';
import redis from '@/lib/redis';
@@ -31,12 +31,15 @@ export async function POST(request: Request) {
const { id, role, createdAt } = user;
// Bind token to password hash so a password change invalidates old tokens.
const pwd = hash(user.password);
let token: string;
if (redis.enabled) {
token = await saveAuth({ userId: id, role });
token = await saveAuth({ userId: id, role, pwd });
} else {
token = createSecureToken({ userId: user.id, role }, secret());
token = createSecureToken({ userId: user.id, role, pwd }, secret());
}
const teams = await getAllUserTeams(id);
+4 -1
View File
@@ -1,7 +1,9 @@
import { saveAuth } from '@/lib/auth';
import { hash } from '@/lib/crypto';
import redis from '@/lib/redis';
import { parseRequest } from '@/lib/request';
import { json, serverError } from '@/lib/response';
import { getUser } from '@/queries/prisma';
export async function POST(request: Request) {
const { auth, error } = await parseRequest(request);
@@ -14,7 +16,8 @@ export async function POST(request: Request) {
return serverError('Redis is disabled');
}
const token = await saveAuth({ userId: auth.user.id }, 86400);
const user = await getUser(auth.user.id, { includePassword: true });
const token = await saveAuth({ userId: auth.user.id, pwd: hash(user.password) }, 86400);
return json({ user: auth.user, token });
}