mirror of
https://github.com/freedbygrace/DynamoDNS.git
synced 2026-07-26 11:38:13 +00:00
Fix API token creation error by correcting date formatting and adding default permissions logic.
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/29294a5b-df23-4880-bdc0-45834e9d7395.jpg
This commit is contained in:
@@ -126,7 +126,7 @@ export default function ApiTokensPage() {
|
||||
// Add token mutation
|
||||
const addTokenMutation = useMutation({
|
||||
mutationFn: async (data: z.infer<typeof tokenFormSchema>) => {
|
||||
const tokenData: Partial<InsertApiToken> = {
|
||||
const tokenData: Partial<any> = {
|
||||
name: data.name,
|
||||
organizationId: currentOrganization?.id || "",
|
||||
role: data.role,
|
||||
@@ -157,30 +157,38 @@ export default function ApiTokensPage() {
|
||||
customDate.setHours(23, 59, 59, 999);
|
||||
}
|
||||
|
||||
tokenData.expiresAt = customDate;
|
||||
// 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':
|
||||
tokenData.expiresAt = new Date(now.getTime() + 60 * 60 * 1000);
|
||||
expiresAt = new Date(now.getTime() + 60 * 60 * 1000);
|
||||
break;
|
||||
case '1day':
|
||||
tokenData.expiresAt = new Date(now.getTime() + 24 * 60 * 60 * 1000);
|
||||
expiresAt = new Date(now.getTime() + 24 * 60 * 60 * 1000);
|
||||
break;
|
||||
case '7days':
|
||||
tokenData.expiresAt = new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000);
|
||||
expiresAt = new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000);
|
||||
break;
|
||||
case '30days':
|
||||
tokenData.expiresAt = new Date(now.getTime() + 30 * 24 * 60 * 60 * 1000);
|
||||
expiresAt = new Date(now.getTime() + 30 * 24 * 60 * 60 * 1000);
|
||||
break;
|
||||
case '90days':
|
||||
tokenData.expiresAt = new Date(now.getTime() + 90 * 24 * 60 * 60 * 1000);
|
||||
expiresAt = new Date(now.getTime() + 90 * 24 * 60 * 60 * 1000);
|
||||
break;
|
||||
case '1year':
|
||||
tokenData.expiresAt = new Date(now.getTime() + 365 * 24 * 60 * 60 * 1000);
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+40
-14
@@ -971,26 +971,52 @@ export async function registerRoutes(app: Express): Promise<Server> {
|
||||
// Generate a random token
|
||||
const tokenValue = randomBytes(32).toString('hex');
|
||||
|
||||
// Validate permissions
|
||||
const permissionsValidator = z.array(z.enum(["admin", "manager", "user", "readonly"]));
|
||||
// Prepare tokenData with values provided explicitly
|
||||
const { name, role, expiresAt, organizationId, isActive } = req.body;
|
||||
|
||||
// Create schema with additional validation
|
||||
const schema = insertApiTokenSchema
|
||||
.omit({ token: true })
|
||||
.extend({
|
||||
permissions: permissionsValidator,
|
||||
name: z.string().min(1)
|
||||
});
|
||||
// Log what we received for debugging
|
||||
console.log("Token creation request body:", {
|
||||
name, role, expiresAt, organizationId, isActive
|
||||
});
|
||||
|
||||
const validatedData = schema.parse(req.body);
|
||||
// Set default permissions based on the role
|
||||
let permissions = [];
|
||||
if (role === 'admin') {
|
||||
permissions = ['admin', 'manager', 'user', 'readonly'];
|
||||
} else if (role === 'manager') {
|
||||
permissions = ['manager', 'user', 'readonly'];
|
||||
} else if (role === 'user') {
|
||||
permissions = ['user', 'readonly'];
|
||||
} else {
|
||||
permissions = ['readonly'];
|
||||
}
|
||||
|
||||
// Set token value and created by
|
||||
const tokenData = {
|
||||
...validatedData,
|
||||
// Create token data object with all required fields
|
||||
let tokenObj: any = {
|
||||
name: name,
|
||||
token: tokenValue,
|
||||
createdBy: req.user?.id
|
||||
organizationId: organizationId,
|
||||
permissions: permissions,
|
||||
role: role,
|
||||
createdBy: req.user?.id || '1', // Provide a fallback ID for admin
|
||||
isActive: isActive !== undefined ? isActive : true
|
||||
};
|
||||
|
||||
// Handle expires date properly
|
||||
if (expiresAt) {
|
||||
// If it's a string that looks like a date, convert it to a Date object
|
||||
if (typeof expiresAt === 'string' && expiresAt.match(/^\d{4}-\d{2}-\d{2}/)) {
|
||||
tokenObj.expiresAt = new Date(expiresAt);
|
||||
} else if (expiresAt instanceof Date) {
|
||||
tokenObj.expiresAt = expiresAt;
|
||||
}
|
||||
}
|
||||
|
||||
// Final tokenData
|
||||
const tokenData = tokenObj;
|
||||
|
||||
console.log("Final token data for storage:", tokenData);
|
||||
|
||||
const token = await storage.createApiToken(tokenData);
|
||||
|
||||
// Return the full token only on creation
|
||||
|
||||
Reference in New Issue
Block a user