From 063a9314883ff1c6f072ade41748f90f7f7e8cda Mon Sep 17 00:00:00 2001 From: balaharisankar Date: Sat, 10 Aug 2024 18:10:57 +0530 Subject: [PATCH] Update password added --- backend/controllers/userController.js | 18 ++++ backend/routes/userRouter.js | 1 + frontend/src/pages/Settings.js | 126 ++++++++++++++++++++++++-- 3 files changed, 135 insertions(+), 10 deletions(-) diff --git a/backend/controllers/userController.js b/backend/controllers/userController.js index 45bbd0b..c1db779 100644 --- a/backend/controllers/userController.js +++ b/backend/controllers/userController.js @@ -3,6 +3,7 @@ const catchAsync=require('../utils/catchAsync'); const AppError = require('../utils/AppError'); const Chat=require('../models/chatModel'); const multer=require('multer'); +const bcrypt=require('bcryptjs'); const multerstorage=multer.diskStorage({ destination:(req,file,cb)=>{ @@ -87,6 +88,23 @@ exports.UploadPhoto=catchAsync(async(req,res,next)=>{ }) }) +exports.updatePassword=catchAsync(async(req,res,next)=>{ + if(!req.body.password) + next(new AppError('Password field is not allowed to be empty',400)) + + try{ + const encryptPassword=await bcrypt.hash(req.body.password,12) + const updatedUser = await User.findByIdAndUpdate({_id:req.user.id},{password:encryptPassword}); + }catch(err){ + res.json({err}) + } + + res.status(200).json({ + status:'success', + message:"Password Updated" + }) +}) + // exports.AvailableUsersToCreateGroup=catchAsync(async(req,res)=>{ diff --git a/backend/routes/userRouter.js b/backend/routes/userRouter.js index ae9debe..c42c375 100644 --- a/backend/routes/userRouter.js +++ b/backend/routes/userRouter.js @@ -12,6 +12,7 @@ router.post('/login',authController.login); router.post('/ispresent',authController.isUserPresent) router.post('/protect',authController.protect,authController.send); router.post('/uploadPhoto',authController.protect,userController.uploadUserPhoto,userController.UploadPhoto) +router.post('/updatepassword',authController.protect,userController.updatePassword) module.exports=router; \ No newline at end of file diff --git a/frontend/src/pages/Settings.js b/frontend/src/pages/Settings.js index 8912912..11f1b34 100644 --- a/frontend/src/pages/Settings.js +++ b/frontend/src/pages/Settings.js @@ -2,23 +2,67 @@ import React from "react"; import Profile from "../components/SettingsComponents/Profile"; import InputName from "../components/SettingsComponents/InputName"; import InputEmail from "../components/SettingsComponents/InputEmail"; -import { useState,useEffect} from "react"; -import {setUser} from '../services/Actions/User/actions' +import { TextField } from "@mui/material"; +import { useState, useEffect } from "react"; +import { setUser } from '../services/Actions/User/actions' import { useDispatch } from 'react-redux'; import { ToastContainer, toast } from "react-toastify"; import InfoIcon from '@mui/icons-material/Info'; +import CheckCircleIcon from '@mui/icons-material/CheckCircle'; +import CancelIcon from '@mui/icons-material/Cancel'; + +const PasswordRequirements = ({ password }) => { + const specialCharacterRegex = /[!@#$%^&*(),.?":{}|<>]/; + const numberRegex = /[0-9]/; + const uppercaseRegex = /[A-Z]/; + + const boxStyle = { + border: '1px solid #ccc', + padding: '8px', + borderRadius: '4px', + marginTop: '8px', + backgroundColor: '#f9f9f9', + width: "40%" + }; + + return ( +
+ +
+ ); +}; export default function Settings() { - const dispatch=useDispatch(); + const dispatch = useDispatch(); const storedData = JSON.parse(localStorage.getItem("info")); const [name, setName] = useState(storedData.name); const [email, setEmail] = useState(storedData.email); + const [password, setPassword] = useState("") + const [confirmPassword, setConfirmPassword] = useState("") + const [showPasswordRequirements, setShowPasswordRequirements] = useState(false); const resetData = () => { setName(storedData.name); setEmail(storedData.email); }; + const resetPassword = () => { + setPassword("") + setConfirmPassword("") + } const notify = (value) => { if (value === "error") return toast.error("Someting went wrong!", { @@ -44,6 +88,34 @@ export default function Settings() { }); }; + const updatePasswordHandler = async () => { + setShowPasswordRequirements(true) + if (password != confirmPassword) { + toast.error("Password and Confirm password not matched"); + } + else { + const cookie = localStorage.getItem("jwt"); + const response = await fetch( + `${process.env.REACT_APP_API_URL}/api/v1/users/updatepassword`, + { + method: "post", + headers: { + "Content-type": "application/json", + Authorization: `Bearer ${cookie}`, + }, + body: JSON.stringify({password}), + } + ); + const data = await response.json(); + if (data.status === "success") { + toast.success("Password updated!") + resetPassword() + setShowPasswordRequirements(false) + } + + else toast.error("Error"); + } + } const updateHandler = () => { const dataSent = { name, @@ -63,20 +135,18 @@ export default function Settings() { } ); const data = await response.json(); - if (data.status === "success") - { + if (data.status === "success") { notify("success"); dispatch(setUser(data.updatedUser)); } - + else notify("error"); }; updateData(); }; return ( -
-
+
@@ -84,8 +154,8 @@ export default function Settings() { Public profile
- -
To update your profile picture, select an image and upload it.
+ +
To update your profile picture, select an image and upload it.
@@ -107,6 +177,42 @@ export default function Settings() {
+
+
+ Update Password +
+
+ { setPassword(e.target.value) }} + value={password} + style={{ width: '40%' }} + /> + {showPasswordRequirements && } + { setConfirmPassword(e.target.value) }} + value={confirmPassword} + style={{ width: '40%' }} + /> +
+
+
+ Update +
+
+ Reset +
+
+
);