mirror of
https://github.com/freedbygrace/DynamoDNS.git
synced 2026-08-23 15:59:02 +00:00
Fix API token creation date/time handling
Replit-Commit-Author: Agent Replit-Commit-Session-Id: 9111ef36-26c8-4085-84ca-a35dc1fec1b5 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/7083d608-d6d3-4a6a-9a27-6286c5109627/96d81055-fe81-465a-89ec-1c16537bde7f.jpg
This commit is contained in:
@@ -126,72 +126,36 @@ export default function ApiTokensPage() {
|
|||||||
// Add token mutation
|
// Add token mutation
|
||||||
const addTokenMutation = useMutation({
|
const addTokenMutation = useMutation({
|
||||||
mutationFn: async (data: z.infer<typeof tokenFormSchema>) => {
|
mutationFn: async (data: z.infer<typeof tokenFormSchema>) => {
|
||||||
const tokenData: Partial<any> = {
|
// Instead of using expiresAt directly, send the form data and let the server handle it
|
||||||
|
const tokenData: any = {
|
||||||
name: data.name,
|
name: data.name,
|
||||||
organizationId: currentOrganization?.id || "",
|
organizationId: currentOrganization?.id || "1",
|
||||||
role: data.role,
|
role: data.role,
|
||||||
isActive: true,
|
isActive: true,
|
||||||
|
expiresIn: data.expiresIn || 'never',
|
||||||
};
|
};
|
||||||
|
|
||||||
// Calculate expiration date based on selection
|
// Add custom date and time fields if present
|
||||||
if (data.expiresIn && data.expiresIn !== 'never') {
|
if (data.expiresIn === 'custom') {
|
||||||
const now = new Date();
|
// For custom date/time, pass as separate fields for the server to process
|
||||||
|
if (data.customDate) {
|
||||||
|
// Format the date as YYYY-MM-DD for the server
|
||||||
|
tokenData.customDate = format(data.customDate, 'yyyy-MM-dd');
|
||||||
|
console.log("Sending customDate:", tokenData.customDate);
|
||||||
|
|
||||||
if (data.expiresIn === 'custom' && data.customDate) {
|
// Add custom time if present
|
||||||
// For custom date/time
|
if (data.customTime) {
|
||||||
if (data.customDate) {
|
tokenData.customTime = data.customTime;
|
||||||
const customDate = new Date(data.customDate);
|
console.log("Sending customTime:", data.customTime);
|
||||||
|
} else {
|
||||||
// If time was selected, add it to the date
|
// Default to end of day if no time
|
||||||
if (data.customTime) {
|
tokenData.customTime = "23:59";
|
||||||
const [hours, minutes] = data.customTime.split(':').map(Number);
|
console.log("Using default end of day time");
|
||||||
// Check if hours and minutes are valid numbers
|
|
||||||
if (!isNaN(hours) && !isNaN(minutes)) {
|
|
||||||
customDate.setHours(hours, minutes, 0, 0);
|
|
||||||
} else {
|
|
||||||
// Default to end of day if time format is invalid
|
|
||||||
customDate.setHours(23, 59, 59, 999);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Default to end of day if no time selected
|
|
||||||
customDate.setHours(23, 59, 59, 999);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Server expects ISO string for dates, not JavaScript Date objects
|
|
||||||
tokenData.expiresAt = customDate.toISOString();
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// For preset expiration options
|
|
||||||
let expiresAt: Date;
|
|
||||||
|
|
||||||
switch (data.expiresIn) {
|
|
||||||
case '1hour':
|
|
||||||
expiresAt = new Date(now.getTime() + 60 * 60 * 1000);
|
|
||||||
break;
|
|
||||||
case '1day':
|
|
||||||
expiresAt = new Date(now.getTime() + 24 * 60 * 60 * 1000);
|
|
||||||
break;
|
|
||||||
case '7days':
|
|
||||||
expiresAt = new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000);
|
|
||||||
break;
|
|
||||||
case '30days':
|
|
||||||
expiresAt = new Date(now.getTime() + 30 * 24 * 60 * 60 * 1000);
|
|
||||||
break;
|
|
||||||
case '90days':
|
|
||||||
expiresAt = new Date(now.getTime() + 90 * 24 * 60 * 60 * 1000);
|
|
||||||
break;
|
|
||||||
case '1year':
|
|
||||||
expiresAt = new Date(now.getTime() + 365 * 24 * 60 * 60 * 1000);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
expiresAt = new Date(now.getTime() + 30 * 24 * 60 * 60 * 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Server expects ISO string for dates, not JavaScript Date objects
|
|
||||||
tokenData.expiresAt = expiresAt.toISOString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log("Sending token data to server:", tokenData);
|
||||||
const res = await apiRequest("POST", "/api/api-tokens", tokenData);
|
const res = await apiRequest("POST", "/api/api-tokens", tokenData);
|
||||||
return await res.json();
|
return await res.json();
|
||||||
},
|
},
|
||||||
|
|||||||
+49
-24
@@ -988,44 +988,67 @@ export async function registerRoutes(app: Express): Promise<Server> {
|
|||||||
permissions: [] // Will be populated in storage.ts
|
permissions: [] // Will be populated in storage.ts
|
||||||
};
|
};
|
||||||
|
|
||||||
// Handle expiration date
|
// Handle expiration date based on the expiresIn parameter
|
||||||
if (req.body.expiresIn && req.body.expiresIn !== 'never') {
|
const expiresIn = req.body.expiresIn || 'never';
|
||||||
|
|
||||||
|
if (expiresIn !== 'never') {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
|
|
||||||
if (req.body.expiresIn === 'custom' && req.body.customDate) {
|
if (expiresIn === 'custom') {
|
||||||
try {
|
try {
|
||||||
console.log("Custom date received:", req.body.customDate);
|
if (req.body.customDate) {
|
||||||
console.log("Custom time received:", req.body.customTime || "none");
|
console.log("Custom date received:", req.body.customDate);
|
||||||
|
|
||||||
// Create date from input
|
// Parse date (format is YYYY-MM-DD from client)
|
||||||
const customDate = new Date(req.body.customDate);
|
const [year, month, day] = req.body.customDate.split('-').map(Number);
|
||||||
console.log("Parsed customDate:", customDate);
|
|
||||||
|
|
||||||
// If time was selected, add it to the date
|
if (isNaN(year) || isNaN(month) || isNaN(day)) {
|
||||||
if (req.body.customTime) {
|
throw new Error("Invalid date format. Expected YYYY-MM-DD");
|
||||||
const [hours, minutes] = req.body.customTime.split(':').map(Number);
|
}
|
||||||
if (!isNaN(hours) && !isNaN(minutes)) {
|
|
||||||
customDate.setHours(hours, minutes, 0, 0);
|
// Create date (months are 0-indexed in JS Date)
|
||||||
console.log(`Setting time to ${hours}:${minutes}`);
|
const customDate = new Date(year, month - 1, day);
|
||||||
|
console.log("Parsed customDate:", customDate);
|
||||||
|
|
||||||
|
// Add time if specified
|
||||||
|
if (req.body.customTime) {
|
||||||
|
console.log("Custom time received:", req.body.customTime);
|
||||||
|
const [hours, minutes] = req.body.customTime.split(':').map(Number);
|
||||||
|
|
||||||
|
if (!isNaN(hours) && !isNaN(minutes)) {
|
||||||
|
customDate.setHours(hours, minutes, 0, 0);
|
||||||
|
console.log(`Setting time to ${hours}:${minutes}`);
|
||||||
|
} else {
|
||||||
|
customDate.setHours(23, 59, 59, 999);
|
||||||
|
console.log("Invalid time format, setting to end of day");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
customDate.setHours(23, 59, 59, 999);
|
customDate.setHours(23, 59, 59, 999);
|
||||||
console.log("Invalid time format, setting to end of day");
|
console.log("No time specified, setting to end of day");
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
customDate.setHours(23, 59, 59, 999);
|
|
||||||
console.log("No time specified, setting to end of day");
|
|
||||||
}
|
|
||||||
|
|
||||||
tokenData.expiresAt = customDate;
|
// Validate the date is in the future
|
||||||
console.log("Final custom expiration date:", customDate);
|
if (customDate < now) {
|
||||||
|
throw new Error("Expiration date must be in the future");
|
||||||
|
}
|
||||||
|
|
||||||
|
tokenData.expiresAt = customDate;
|
||||||
|
console.log("Final custom expiration date:", customDate.toISOString());
|
||||||
|
} else {
|
||||||
|
throw new Error("Custom date is required for custom expiration");
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("Error parsing custom date:", e);
|
console.error("Error parsing custom date:", e);
|
||||||
|
return res.status(400).json({
|
||||||
|
message: "Invalid custom date or time",
|
||||||
|
details: e instanceof Error ? e.message : String(e)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// For preset expiration options
|
// For preset expiration options
|
||||||
let expirationDate: Date | null = null;
|
let expirationDate: Date | null = null;
|
||||||
|
|
||||||
switch (req.body.expiresIn) {
|
switch (expiresIn) {
|
||||||
case '1hour':
|
case '1hour':
|
||||||
expirationDate = new Date(now.getTime() + 60 * 60 * 1000);
|
expirationDate = new Date(now.getTime() + 60 * 60 * 1000);
|
||||||
break;
|
break;
|
||||||
@@ -1044,11 +1067,13 @@ export async function registerRoutes(app: Express): Promise<Server> {
|
|||||||
case '1year':
|
case '1year':
|
||||||
expirationDate = new Date(now.getTime() + 365 * 24 * 60 * 60 * 1000);
|
expirationDate = new Date(now.getTime() + 365 * 24 * 60 * 60 * 1000);
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
console.log(`Unrecognized expiresIn value: ${expiresIn}, defaulting to no expiration`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (expirationDate) {
|
if (expirationDate) {
|
||||||
tokenData.expiresAt = expirationDate;
|
tokenData.expiresAt = expirationDate;
|
||||||
console.log(`Setting expiration to ${req.body.expiresIn}:`, expirationDate);
|
console.log(`Setting expiration to ${expiresIn}:`, expirationDate.toISOString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -1066,7 +1091,7 @@ export async function registerRoutes(app: Express): Promise<Server> {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error creating API token:", error);
|
console.error("Error creating API token:", error);
|
||||||
// Return detailed error to help debugging
|
// Return detailed error to help debugging
|
||||||
res.status(500).json({
|
res.status(400).json({
|
||||||
message: "Error creating token",
|
message: "Error creating token",
|
||||||
error: error instanceof Error ? error.message : String(error),
|
error: error instanceof Error ? error.message : String(error),
|
||||||
stack: error instanceof Error ? error.stack : undefined
|
stack: error instanceof Error ? error.stack : undefined
|
||||||
|
|||||||
Reference in New Issue
Block a user