node-mongo-demo

node.js and mongodb demo

git clone https://9o.is/git/node-mongo-demo.git

PasswordSettings.js

(3171B)


      1 import React, { useEffect, useState } from "react";
      2 import {
      3   Avatar,
      4   Button,
      5   Container,
      6   Grid,
      7   Paper,
      8   Typography,
      9 } from "@mui/material";
     10 import Input from "../Login/Input";
     11 import { styles } from "./styles";
     12 import LockIcon from "@mui/icons-material/LockRounded";
     13 import { changePassword } from "../../actions/login";
     14 import { jwtDecode } from "jwt-decode";
     15 import { useNavigate } from "react-router-dom";
     16 import { useDispatch } from "react-redux";
     17 
     18 const PasswordSetting = () => {
     19   const user = localStorage.getItem("profile")
     20     ? jwtDecode(JSON.parse(localStorage.getItem("profile")).token)
     21     : "null";
     22   const isSingedIn = user;
     23   const history = useNavigate();
     24   const [showPassword, setShowPassword] = useState(false);
     25   const [changeFormData, setChangeFormData] = useState({
     26     oldPassword: "",
     27     newPassword: "",
     28     email: user.email,
     29   });
     30   const dispatch = useDispatch();
     31 
     32   const handleChangeC = (e) => {
     33     setChangeFormData({ ...changeFormData, [e.target.name]: e.target.value });
     34   };
     35 
     36   const handleShowPassword = (e) => {
     37     setShowPassword((prevPassword) => !prevPassword);
     38   };
     39 
     40   const handleSubmitChange = (e) => {
     41     e.preventDefault();
     42     dispatch(changePassword(changeFormData, history));
     43   };
     44 
     45   useEffect(() => {
     46     if (isSingedIn == "null" || isSingedIn === null) {
     47       history("/");
     48     }
     49   }, []);
     50 
     51   if (isSingedIn !== "null" && isSingedIn !== null) {
     52     return (
     53       <div>
     54         <Container component="main" maxWidth="xs">
     55           <Paper sx={styles.paper} elevation={3}>
     56             <Avatar sx={styles.avatar}>
     57               <LockIcon />
     58             </Avatar>
     59             <Typography variant="h5" color="primary">
     60               Set Password
     61             </Typography>
     62             <form sx={styles.form} onSubmit={handleSubmitChange}>
     63               <Grid container spacing={2}>
     64                 <Typography
     65                   variant="caption"
     66                   color="error"
     67                   sx={styles.typo}
     68                   align="left"
     69                 >
     70                   To change your password, enter your current password and your new password.
     71                 </Typography>
     72                 <Input
     73                   name="oldPassword"
     74                   label="Current Password"
     75                   handleChange={handleChangeC}
     76                   type={showPassword ? "text" : "password"}
     77                   handleShowPassword={handleShowPassword}
     78                 />
     79                 <Input
     80                   name="newPassword"
     81                   label="New Password"
     82                   handleChange={handleChangeC}
     83                   type="password"
     84                   showBar={true}
     85                   passValue={changeFormData.newPassword}
     86                 />
     87                 <Button
     88                   type="submit"
     89                   sx={styles.submit}
     90                   fullWidth
     91                   variant="contained"
     92                   color="primary"
     93                 >
     94                   Change Password
     95                 </Button>
     96               </Grid>
     97             </form>
     98           </Paper>
     99         </Container>
    100       </div>
    101     );
    102   } else {
    103     return <>No Access</>;
    104   }
    105 };
    106 
    107 export default PasswordSetting;