import {NextApiRequest, NextApiResponse} from "next";
import {getAuth, sendPasswordResetEmail} from "firebase/auth";
import {app} from "@/firebase";
import {sessionOptions} from "@/lib/session";
import {withIronSessionApiRoute} from "iron-session/next";

const auth = getAuth(app);

export default withIronSessionApiRoute(reset, sessionOptions);

async function reset(req: NextApiRequest, res: NextApiResponse) {
	const {email} = req.body as {email: string};

	sendPasswordResetEmail(auth, email)
		.then(() => res.status(200).json({ok: true}))
		.catch(() => res.status(404).json({ok: false}));
}
