awekas/apps/mqtt-gateway/src/middleware/validateParams.ts
Felix Dürrwald 94a95876dd
Some checks failed
CI / main (push) Failing after 11s
feat: Implement mqtt-gateway
2024-09-29 08:48:44 +02:00

25 lines
934 B
TypeScript

import { NextFunction, Request, Response } from "express"
import { WSQuery } from "../types"
// Validate input parameters
const validateParamsMiddleware = (MPLABS_ID: string, MPLABS_PASSWORD: string) => (req: Request, res: Response, next: NextFunction) => {
const { ID, PASSWORD, action, dateutc } = req.query as WSQuery
// Validate action and dateutc
if (action !== 'updateraww') {
res.status(400).send('Invalid action')
} else if (dateutc !== 'now' && isNaN(Date.parse(dateutc))) {
res.status(400).send('Invalid dateutc format')
}
// Validate ID and PASSWORD (this is where you would verify against your database)
// For now, we'll mock this validation
else if (ID !== MPLABS_ID || PASSWORD !== MPLABS_PASSWORD) {
return res.status(401).send('INVALIDPASSWORDID|Password and/or id are incorrect')
} else {
next()
}
}
export default validateParamsMiddleware