You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constcustomValidators={isStrongPassword: (value: string)=>{consthasUpperCase=/[A-Z]/.test(value);consthasLowerCase=/[a-z]/.test(value);consthasNumbers=/\d/.test(value);consthasSpecialChar=/[!@#$%^&*(),.?":{}|<>]/.test(value);if(!hasUpperCase||!hasLowerCase||!hasNumbers||!hasSpecialChar){return'Password must contain uppercase, lowercase, number, and special character';}returntrue;},isAdult: (value: string)=>{constage=parseInt(value);consttoday=newDate();constbirthDate=newDate(value);constageDiff=today.getFullYear()-birthDate.getFullYear();returnageDiff>=18||'Must be at least 18 years old';}};constregistrationRules={username: 'required|min:3|max:20|unique:users,username',email: 'required|email|unique:users,email',password: 'required|min:8|isStrongPassword',confirmPassword: 'required|same:password',birthDate: 'required|date|isAdult',terms: 'required|accepted'};app.post('/register',async(req: Request,res)=>{constresult=awaitvalidateInput(req,{
...registrationRules,
customValidators
});if(result.failed){returnres.status(400).json({errors: result.errors});}// Create user account...});
constapiKeyRules={'x-api-key': 'required|exists:api_keys,key','x-user-id': 'required|exists:users,id'};app.use('/api/*',async(req: Request,res,next)=>{constresult=awaitvalidateInput(req,apiKeyRules);if(result.failed){returnres.status(401).json({error: 'Invalid API credentials',details: result.errors});}// Add user info to requestreq.user=result.data;next();});