Created
August 29, 2022 15:08
-
-
Save KolbySisk/16c862320ad27fc4923fae3e30547065 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { NextApiRequest, NextApiResponse } from 'next'; | |
type User = { userId: string }; | |
const allowedMethods = ['GET']; | |
const formSchema = z | |
.object({ | |
email: z.string().email(), | |
password: z.string().min(4), | |
passwordConfirm: z.string().min(4), | |
}) | |
.refine((data) => data.password === data.passwordConfirm, { | |
message: "Passwords don't match", | |
}); | |
const handler = (req: NextApiRequest, res: NextApiResponse<User>) => { | |
try { | |
if (!allowedMethods.includes(req.method!) || req.method == 'OPTIONS') { | |
return res.status(405).send({ message: 'Method not allowed.' }); | |
} | |
// Validate the incoming data and return 400 if it's not what is expected | |
const body = typeof req.body === 'object' ? req.body : JSON.parse(req.body); | |
const validation = formSchema.safeParse(body); | |
if (!validation.success) { | |
return res.status(400).send({ | |
message: validation.error, | |
}); | |
} | |
return res.status(200).json({ userId: 'abc123' }); | |
} catch (error) { | |
console.error(error); | |
Sentry.captureException(error); | |
res.status(500).send({ message: 'Server error!' }); | |
} | |
}; | |
export default handler; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment