Hello everyone,
I'm fairly new to the framework and have a few questions regarding API routes with the NextJS application.
It's fairly simple, a user submits an email to be notified of updates. That email gets stored in a firebase database. There is no sign in for this application so it is public. I've created my API route in the folder structure pages > api > consent > email
import type { NextApiRequest, NextApiResponse } from 'next'
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === 'POST') {
try{
const dbReference = db.database().ref(`/marketing/awesome-sauce`)
const result = res.json({
message: "Email Successfully Saved"
})
const { email } = req.body
await dbReference.update({
email: email,
})
res.status(200).json({ result })
} catch(error) {
console.error(error)
res.status(500).json({error: "Internal Server Error"})
}
} else {
const result = res.json({
error: "HTTP Method not Supported"
})
res.status(500).send({result})
}
}
Pretty boiler plate code. I need to make sure that no one can do anything malicious if they know what the route is. I'm looking for a way to protect this route, maybe have it only allow the route to be called if it's coming from the application? I'm assuming there is a middleware solution but am having trouble finding documentation on doing something like this. Everything I find deals with a user authorization and sign in.