Spaces:
Sleeping
Sleeping
Download backend/src/utils/validators.js from monumkp89/pixelcraft: direct link, hf CLI and curl.
- Browser
- Download file 3.3 kB
-
https://huggingface.co/spaces/monumkp89/pixelcraft/resolve/main/backend/src/utils/validators.js
- Command line
-
hf download hf://spaces/monumkp89/pixelcraft/backend/src/utils/validators.js
-
curl -L -o validators.js https://huggingface.co/spaces/monumkp89/pixelcraft/resolve/main/backend/src/utils/validators.js
3.3 kB
| const jwt = require('jsonwebtoken'); | |
| const validator = require('validator'); | |
| const PHONE_REGEX = /^\d{10,15}$/; | |
| const getJwtSecret = () => { | |
| const secret = process.env.JWT_SECRET; | |
| if (!secret) { | |
| console.warn('JWT_SECRET not set - using insecure fallback. Set JWT_SECRET in environment for production.'); | |
| return 'fallback_secret_change_me_in_production'; | |
| } | |
| return secret; | |
| }; | |
| const generateToken = (userId) => { | |
| return jwt.sign({ userId }, getJwtSecret(), { expiresIn: '7d' }); | |
| }; | |
| const normalizeEmail = (email = '') => String(email || '').trim().toLowerCase(); | |
| const normalizePhone = (phone = '') => String(phone || '').trim().replace(/[^\d]/g, ''); | |
| const validateEmail = (email) => { | |
| const normalizedEmail = normalizeEmail(email); | |
| return Boolean(normalizedEmail) && validator.isEmail(normalizedEmail); | |
| }; | |
| const validatePhone = (phone) => { | |
| const normalizedPhone = normalizePhone(phone); | |
| return PHONE_REGEX.test(normalizedPhone); | |
| }; | |
| const getSmsDefaultCountryCode = () => | |
| String(process.env.SMS_DEFAULT_COUNTRY_CODE || '') | |
| .trim() | |
| .replace(/[^\d]/g, ''); | |
| const getContactFromIdentifier = (identifier) => { | |
| const rawIdentifier = String(identifier || '').trim(); | |
| if (!rawIdentifier) { | |
| return null; | |
| } | |
| const normalizedEmail = normalizeEmail(rawIdentifier); | |
| if (validateEmail(normalizedEmail)) { | |
| return { | |
| type: 'email', | |
| value: normalizedEmail, | |
| label: normalizedEmail, | |
| }; | |
| } | |
| const normalizedPhone = normalizePhone(rawIdentifier); | |
| if (validatePhone(normalizedPhone)) { | |
| return { | |
| type: 'phone', | |
| value: normalizedPhone, | |
| label: normalizedPhone, | |
| }; | |
| } | |
| return null; | |
| }; | |
| const buildContactFields = (contact) => ({ | |
| email: contact?.type === 'email' ? contact.value : undefined, | |
| phone: contact?.type === 'phone' ? contact.value : undefined, | |
| }); | |
| const formatPhoneForSms = (phone) => { | |
| const normalizedPhone = normalizePhone(phone); | |
| if (!normalizedPhone) { | |
| return ''; | |
| } | |
| const defaultCountryCode = getSmsDefaultCountryCode(); | |
| if (normalizedPhone.length === 10) { | |
| if (!defaultCountryCode) { | |
| const error = new Error( | |
| 'For SMS delivery, enter the mobile number with country code (for example, +919876543210) or configure SMS_DEFAULT_COUNTRY_CODE on the server.' | |
| ); | |
| error.status = 400; | |
| throw error; | |
| } | |
| return `+${defaultCountryCode}${normalizedPhone}`; | |
| } | |
| return `+${normalizedPhone}`; | |
| }; | |
| const validatePassword = (password) => { | |
| const re = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d@$!%*?&]{8,}$/; | |
| return re.test(password); | |
| }; | |
| const getPasswordStrength = (password) => { | |
| let strength = 0; | |
| if (password.length >= 8) strength++; | |
| if (password.length >= 12) strength++; | |
| if (/[a-z]/.test(password)) strength++; | |
| if (/[A-Z]/.test(password)) strength++; | |
| if (/\d/.test(password)) strength++; | |
| if (/[@$!%*?&]/.test(password)) strength++; | |
| if (strength <= 2) return 'Weak'; | |
| if (strength <= 4) return 'Fair'; | |
| if (strength <= 5) return 'Good'; | |
| return 'Strong'; | |
| }; | |
| module.exports = { | |
| buildContactFields, | |
| formatPhoneForSms, | |
| generateToken, | |
| getContactFromIdentifier, | |
| getPasswordStrength, | |
| getSmsDefaultCountryCode, | |
| normalizeEmail, | |
| normalizePhone, | |
| validateEmail, | |
| validatePassword, | |
| validatePhone, | |
| }; | |