feat(user-auth): improve token refresh logic and default values

This commit is contained in:
carlosmonastyrski
2025-04-29 18:28:18 -03:00
parent 024ed0c0d8
commit 7bd61d88fc
9 changed files with 79 additions and 12 deletions
+4 -3
View File
@@ -2,6 +2,7 @@ import jwt from "jsonwebtoken";
import { z } from "zod";
import { getConfig } from "@app/lib/config/env";
import { getMinExpiresIn } from "@app/lib/fn";
import { authRateLimit, writeLimit } from "@app/server/config/rateLimiter";
import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
import { AuthMode, AuthTokenType } from "@app/services/auth/auth-type";
@@ -79,7 +80,7 @@ export const registerAuthRoutes = async (server: FastifyZodProvider) => {
handler: async (req) => {
const { decodedToken, tokenVersion } = await server.services.authToken.validateRefreshToken(req.cookies.jid);
const appCfg = getConfig();
let expiresIn = appCfg.JWT_AUTH_LIFETIME;
let expiresIn: string | number = appCfg.JWT_AUTH_LIFETIME;
if (decodedToken.organizationId) {
const org = await server.services.org.findOrganizationById(
decodedToken.userId,
@@ -87,8 +88,8 @@ export const registerAuthRoutes = async (server: FastifyZodProvider) => {
decodedToken.authMethod,
decodedToken.organizationId
);
if (org) {
expiresIn = org.userTokenExpiration;
if (org && org.userTokenExpiration) {
expiresIn = getMinExpiresIn(appCfg.JWT_AUTH_LIFETIME, org.userTokenExpiration);
}
}
@@ -267,7 +267,7 @@ export const registerOrgRouter = async (server: FastifyZodProvider) => {
bypassOrgAuthEnabled: z.boolean().optional(),
userTokenExpiration: z
.string()
.regex(new RE2(/^\d+[mhdw]$/), "Must be a number followed by m, h, d, or w")
.refine((val) => new RE2(/^\d+[mhdw]$/).test(val), "Must be a number followed by m, h, d, or w")
.refine(
(val) => {
const numericPart = val.slice(0, -1);