mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-15 00:52:09 +00:00
feat(user-auth): improve token refresh logic and default values
This commit is contained in:
@@ -6,4 +6,5 @@ export * from "./array";
|
||||
export * from "./dates";
|
||||
export * from "./object";
|
||||
export * from "./string";
|
||||
export * from "./time";
|
||||
export * from "./undefined";
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
const convertToMilliseconds = (exp: string | number): number => {
|
||||
if (typeof exp === "number") {
|
||||
return exp * 1000;
|
||||
}
|
||||
|
||||
const match = exp.match(/^(\d+)\s*([a-z]*)$/i);
|
||||
if (!match) {
|
||||
throw new Error(`Invalid expiration format: ${exp}`);
|
||||
}
|
||||
|
||||
const value = parseInt(match[1], 10);
|
||||
const unit = match[2].toLowerCase();
|
||||
|
||||
switch (unit) {
|
||||
case "":
|
||||
case "s":
|
||||
return value * 1000; // seconds
|
||||
case "m":
|
||||
return value * 60 * 1000; // minutes
|
||||
case "h":
|
||||
return value * 60 * 60 * 1000; // hours
|
||||
case "d":
|
||||
return value * 24 * 60 * 60 * 1000; // days
|
||||
default:
|
||||
throw new Error(`Unsupported time unit: ${unit}`);
|
||||
}
|
||||
};
|
||||
|
||||
export const getMinExpiresIn = (exp1: string | number, exp2: string | number): string | number => {
|
||||
const ms1 = convertToMilliseconds(exp1);
|
||||
const ms2 = convertToMilliseconds(exp2);
|
||||
|
||||
return ms1 <= ms2 ? exp1 : exp2;
|
||||
};
|
||||
Reference in New Issue
Block a user