87 lines
2.2 KiB
JavaScript
87 lines
2.2 KiB
JavaScript
// Extract ID from NocoDB response
|
|
function extractId(record) {
|
|
return record.Id || record.id || record.ID || record._id;
|
|
}
|
|
|
|
// Sanitize user data for response
|
|
function sanitizeUser(user) {
|
|
const { Password, password, ...safeUser } = user;
|
|
return safeUser;
|
|
}
|
|
|
|
// Validate email format
|
|
function validateEmail(email) {
|
|
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
return emailRegex.test(email);
|
|
}
|
|
|
|
// Validate URL format
|
|
function validateUrl(url) {
|
|
if (!url || typeof url !== 'string') {
|
|
return '';
|
|
}
|
|
|
|
const trimmed = url.trim();
|
|
if (!trimmed) {
|
|
return '';
|
|
}
|
|
|
|
// Basic URL validation
|
|
try {
|
|
new URL(trimmed);
|
|
return trimmed;
|
|
} catch (e) {
|
|
// If not a valid URL, check if it's a relative path or missing protocol
|
|
if (trimmed.startsWith('/') || !trimmed.includes('://')) {
|
|
// For relative paths or missing protocol, return as-is
|
|
return trimmed;
|
|
}
|
|
return '';
|
|
}
|
|
}
|
|
|
|
// Generate a random password
|
|
function generatePassword(length = 12) {
|
|
const charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*';
|
|
let password = '';
|
|
for (let i = 0; i < length; i++) {
|
|
password += charset.charAt(Math.floor(Math.random() * charset.length));
|
|
}
|
|
return password;
|
|
}
|
|
|
|
// Clean HTML for plain text
|
|
function stripHtmlTags(html) {
|
|
return html.replace(/<[^>]*>/g, '').replace(/\s+/g, ' ').trim();
|
|
}
|
|
|
|
// Format date for display
|
|
function formatDate(date) {
|
|
if (!date) return '';
|
|
return new Date(date).toLocaleString();
|
|
}
|
|
|
|
// Check if user is expired (for temp users)
|
|
function isUserExpired(user) {
|
|
const userType = user['User Type'] || user.UserType || user.userType || 'user';
|
|
if (userType !== 'temp') return false;
|
|
|
|
const expiration = user.ExpiresAt || user.expiresAt || user.Expiration || user.expiration;
|
|
if (!expiration) return false;
|
|
|
|
const expirationDate = new Date(expiration);
|
|
const now = new Date();
|
|
|
|
return now > expirationDate;
|
|
}
|
|
|
|
module.exports = {
|
|
extractId,
|
|
sanitizeUser,
|
|
validateEmail,
|
|
validateUrl,
|
|
generatePassword,
|
|
stripHtmlTags,
|
|
formatDate,
|
|
isUserExpired
|
|
}; |