import {NextApiRequest, NextApiResponse} from "next";
import {getAuth, confirmPasswordReset} 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(confirm, sessionOptions);

async function confirm(req: NextApiRequest, res: NextApiResponse) {
	const {code, password} = req.body as {code: string; password: string};

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