diff --git a/app/sait-staff/RDashboard/Table.js b/app/sait-staff/RDashboard/Table.js index 30a877a..9e8f921 100644 --- a/app/sait-staff/RDashboard/Table.js +++ b/app/sait-staff/RDashboard/Table.js @@ -1,23 +1,34 @@ -'use client' -import React, { useState } from 'react'; +"use client"; +import React, { useState } from "react"; import { LuPencil } from "react-icons/lu"; import { LuTrash } from "react-icons/lu"; import { IoStorefront } from "react-icons/io5"; -import { formatPhoneNumber } from '@/Constant/formated'; +import { formatPhoneNumber } from "@/Constant/formated"; import { MdOutlineDoneOutline } from "react-icons/md"; import { FaFilter } from "react-icons/fa"; -const Table = ({ restaurants, handleEdit, handleDelete, setIsAdding, setSearch, search }) => { +const Table = ({ + restaurants, + handleEdit, + handleDelete, + setIsAdding, + setSearch, + search, +}) => { const [searchTerm, setSearchTerm] = useState(""); const [searchBy, setSearchBy] = useState("name"); - const filteredRestaurants = + const filteredRestaurants = restaurants && restaurants.length > 0 - ? restaurants.filter(restaurant => { + ? restaurants.filter((restaurant) => { if (searchBy === "name") { - return restaurant.name.toLowerCase().includes(searchTerm.toLowerCase()); + return restaurant.name + .toLowerCase() + .includes(searchTerm.toLowerCase()); } else if (searchBy === "email") { - return restaurant.email.toLowerCase().includes(searchTerm.toLowerCase()); + return restaurant.email + .toLowerCase() + .includes(searchTerm.toLowerCase()); } else if (searchBy === "phone") { return restaurant.phoneNumber.includes(searchTerm); } @@ -68,10 +79,15 @@ const Table = ({ restaurants, handleEdit, handleDelete, setIsAdding, setSearch, )} -
+
- +
- + setSearchTerm(e.target.value)} + />
+
+ + +
+
@@ -32,6 +108,9 @@ const Table = ({ + @@ -47,77 +126,78 @@ const Table = ({ - {admin && admin.length > 0 ? ( - admin.map((user) => ( - - - - + - - - - )) - ) : ( - + + + + + + + + + + ))):(<> - - )} + )}
User + Email + Role
-
-
- -
-
-

- {user.name} -

-
-
-
-

- {user.role} -

-
+ {filteredEmployees.length > 0 ? (filteredEmployees.map((user) => ( +
+
+
+ +
+

- {user.accountCreated.toDate().toDateString()} + {user.name}

-
- { - setIsVisisble(true); - setIsActive(user.active), - setEmail(user.email), - setDocId(user.id); - }} - className={`relative inline-block px-3 py-1 font-semibold leading-tight rounded ${ - user.active ? "bg-green-400" : "bg-red-400" - }`} - > - - - {user.active ? "Active" : "InActive"} - - - - -
+

+ {user.email} +

+
+

+ {user.role} +

+
+

+ {user.accountCreated.toDate().toDateString()} +

+
+ { + setIsVisisble(true); + setIsActive(user.active), + setEmail(user.email), + setDocId(user.id); + }} + className={`relative inline-block px-3 py-1 font-semibold leading-tight rounded ${ + user.active ? "bg-green-400" : "bg-red-400" + }`} + > + + + {user.active ? "Active" : "InActive"} + + + + +
No data available
diff --git a/app/sait-staff/settingS/index.js b/app/sait-staff/settingS/index.js index f83cf1d..7b76139 100644 --- a/app/sait-staff/settingS/index.js +++ b/app/sait-staff/settingS/index.js @@ -1,20 +1,58 @@ /** * v0 by Vercel. */ +"use client"; import { Button } from "@/components/ui/button" import { Card, CardHeader, CardTitle, CardDescription, CardContent, CardFooter } from "@/components/ui/card" import Passwordreset from "./passwordreset/page" import { Checkbox } from "@/components/ui/checkbox" import { Label } from "@/components/ui/label" import { Input } from "@/components/ui/input" +import { getSaitDataByUser } from "@/services/GetRequest/getRequest" +import { useUserAuth } from "@/services/utils" +import { useEffect, useState } from "react"; +import { getAuth } from "firebase/auth"; +import { deleteSaitUser } from "@/services/PostRequest/postRequest"; +import { useRouter } from "next/navigation"; export default function Settings() { + const route = useRouter(); + const [userEmail, setUserEmail] = useState(""); + const { user } = useUserAuth(); + const auth = getAuth(); + const [saitData, setSaitData] = useState(null); + async function getUserData(){ + const data = await getSaitDataByUser(user); + setSaitData(data); + } + useEffect(() => { + if(user){ + getUserData(); + console.log(auth.currentUser) + } + if(user==false){ + route.push("/"); + } + },[user]); + + const handleDeleteAccount = async (e) => { + e.preventDefault(); + if(userEmail !== saitData[0].email){ + alert("Email does not match"); + return; + } + if(userEmail == saitData[0].email){ + await deleteSaitUser(auth.currentUser,saitData[0].id).then(()=>{ + alert("Account deleted successfully"); + })}; + + }; return (
-
- + {saitData ? (
+
@@ -26,10 +64,10 @@ export default function Settings() {

- - + + {setUserEmail(e.target.value)})} id="username" placeholder="Enter your email" />
-
@@ -38,7 +76,12 @@ export default function Settings() {
+
):( +
+

Loading...

+ )} +
) diff --git a/app/sait-staff/settingS/passwordreset/page.js b/app/sait-staff/settingS/passwordreset/page.js index 8328efa..d4b92dc 100644 --- a/app/sait-staff/settingS/passwordreset/page.js +++ b/app/sait-staff/settingS/passwordreset/page.js @@ -11,11 +11,49 @@ import { Label } from "@/components/ui/label" import { Switch } from "@/components/ui/switch" import { Input } from "@/components/ui/input" import { Button } from "@/components/ui/button" +import { updatePassword, EmailAuthProvider, reauthenticateWithCredential } from "firebase/auth"; -export default function Passwordreset() { +export default function Passwordreset({auth,email}) { + const user = auth.currentUser; + const [oldPassword, setOldPassword] = useState(""); + const [newPassword, setNewPassword] = useState(""); + const [confirmPassword, setConfirmPassword] = useState(""); + const credential = EmailAuthProvider.credential(email,oldPassword); + const [showOldPassword, setShowOldPassword] = useState(false) const [showSection, setShowSection] = useState(true) const [showPassword, setShowPassword] = useState(false) const [showPasswordConfirm, setShowPasswordConfirm] = useState(false) + const [passwordError, setPasswordError] = useState('') + + const handlePasswordChange = () =>{ + if(oldPassword === ""|| newPassword === "" || confirmPassword === ""){ + alert("Please fill all fields"); + return; + } + if (newPassword !== confirmPassword){ + alert("Passwords do not match"); + return; + } + if(oldPassword === newPassword){ + alert("New password cannot be the same as old password"); + return; + } + if(!newPassword.match( + /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/ + )){ + alert("Password must contain at least 8 characters, one uppercase, one lowercase, one number and one special character"); + return; + } + reauthenticateWithCredential(user, credential).then(()=>{ + updatePassword(user,newPassword).then(()=>{ + alert("Password updated successfully"); + }).catch((error)=>{ + alert("Error updating password: ",error); + }) + }).catch((error)=>{ + alert("Error reauthenticating user: ",error); + }) + } return ( // Changes to be made here @@ -23,7 +61,7 @@ export default function Passwordreset() {

Change Your Password

- +
@@ -33,13 +71,28 @@ export default function Passwordreset() {
{showSection && (
- +
)} +
+ +
+ {setOldPassword(e.target.value)})} type={showOldPassword ? "text" : "password"} required /> + +
+
- + {setNewPassword(e.target.value)})} type={showPassword ? "text" : "password"} required /> diff --git a/services/PostRequest/postRequest.js b/services/PostRequest/postRequest.js index 3fa67eb..a2a3384 100644 --- a/services/PostRequest/postRequest.js +++ b/services/PostRequest/postRequest.js @@ -327,3 +327,39 @@ export async function deleteRestaurantUser(currentUser, id, userId) { console.log("No current user found"); } } +// to delete sait staff data from database and athentication +export async function deleteSaitUser(currentUser, id) { + if (currentUser) { + try { + // Step 1: Re-authenticate user if necessary + try { + await deleteUser(currentUser); + } catch (error) { + if (error.code === 'auth/requires-recent-login') { + const credential = EmailAuthProvider.credential( + currentUser.email, + prompt("Please enter your password to re-authenticate.") + ); + await reauthenticateWithCredential(currentUser, credential); + await deleteUser(currentUser); + } else { + throw error; + } + } + + // Step 2: Delete user document from Firestore + await deleteDoc(doc(db, "saitStaff", id)); + + alert("Your account has been deleted successfully!"); + + } catch (error) { + if (error.code === 'auth/requires-recent-login') { + alert("To delete your account, please log out first and then proceed with the account deletion."); + } else { + console.error('Error while deleting user:', error); + } + } + } else { + console.log("No current user found"); + } +}