diff --git a/src/components/Forms/UserPassword/index.jsx b/src/components/Forms/UserPassword/index.jsx index a1e505ca..f5fbd221 100644 --- a/src/components/Forms/UserPassword/index.jsx +++ b/src/components/Forms/UserPassword/index.jsx @@ -1,11 +1,70 @@ -import React from "react"; +import React, { useState } from "react"; import useStyles from "./styles"; -import { Box, Card, Typography, Button, Switch } from "@mui/material"; +import { Box, Card, Typography, Button, Switch, Snackbar } from "@mui/material"; import { Input } from "../../ui-helpers/Inputs/SecondaryInput"; +import { auth } from "../../../config/index"; +import { EmailAuthProvider } from "firebase/auth"; const UserPassword = () => { const classes = useStyles(); + const [oldPassword, setOldPassword] = useState(""); + const [newPassword, setNewPassword] = useState(""); + const [confirmPassword, setConfirmPassword] = useState(""); + const [error, setError] = useState(""); + const [successMessage, setSuccessMessage] = useState(null); + const [openSnackbar, setOpenSnackbar] = useState(false); + + const handleSnackbarClose = (event, reason) => { + setOpenSnackbar(false); + }; + + const handleUpdatePassword = () => { + const user = auth.currentUser; + setOpenSnackbar(false); + + if (!user) { + console.log("No user Found"); + setError("No user found"); + return; + } + + if (newPassword !== confirmPassword) { + console.log("New password and confirm password do not match."); + setError("New password and confirm password do not match."); + setOpenSnackbar(true); + return; + } + + const credential = EmailAuthProvider.credential(user.email, oldPassword); + + user.reauthenticateWithCredential(credential) + .then(() => { + setError(null); + + user + .updatePassword(newPassword) + .then(() => { + console.log("password updated successfully."); + setSuccessMessage("Password updated successfully"); + setOpenSnackbar(true); + setOldPassword(""); + setNewPassword(""); + setConfirmPassword(""); + }) + .catch((error) => { + console.error("Error updating password:", error.message); + setError("Failed to update password. Please try again."); + setOpenSnackbar(true); + }); + }) + .catch((error) => { + console.error("Error reauthenticating user:", error.message); + setError("Authentication failed. Please provide the correct current password."); + setOpenSnackbar(true); + }) + }; + return ( { type="password" className={classes.input} data-testId="oldPassword" + value={oldPassword} + onChange={(e) => setOldPassword(e.target.value)} /> @@ -30,6 +91,8 @@ const UserPassword = () => { type="password" className={classes.input} data-testId="newPassword" + value={newPassword} + onChange={(e) => setNewPassword(e.target.value)} /> @@ -38,9 +101,15 @@ const UserPassword = () => { type="password" className={classes.input} data-testId="confirmPassword" + value={confirmPassword} + onChange={(e) => setConfirmPassword(e.target.value)} /> - @@ -67,6 +136,15 @@ const UserPassword = () => { + + + ); }; diff --git a/src/config/index.js b/src/config/index.js index ee402c72..9720d213 100644 --- a/src/config/index.js +++ b/src/config/index.js @@ -86,4 +86,6 @@ export const onMessageListener = () => export const messaging = firebase_messaging; +export const auth = firebase.auth(); + export default firebase;