after login I saved user data in localstorage to authenticate user. The problem is login from different chrome tabs for chatting, user data in localstorage after login is different but while performing some operation all tabs localstorage data is same for example
while login from different tabs user data in localstorage
- chat-user:- {“_id”:”6656efa2df44afd7687f8294″,”fullName”:”rahul kachhi”,”userName”:”rahul”,”profilePic”:”https://avatar.iran.liara.run/public/boy?username=rahul”}
- chat-user:- {“_id”:”6656efa2df44afd7687f8295″,”fullName”:”riya rajak”,”userName”:”riya”,”profilePic”:”https://avatar.iran.liara.run/public/girl?username=riya”}
- chat-user:- {“_id”:”6656efa2df44afd7687f8296″,”fullName”:”Mo.Nafees”,”userName”:”nafees”,”profilePic”:”https://avatar.iran.liara.run/public/boy?username=nafees”}
but after performing some operations in frontend
all chat user data is same in all tabs
import {useState} from 'react'
import toast from 'react-hot-toast';
import { useAuthContext } from '../context/AuthContext';
const useLogin = () => {
const [loading,setLoading]=useState(false);
const {setAuthUser}=useAuthContext();
const login=async(userName,password)=>{
const success=handleInputErrors({userName,password})
if(!success) return;
setLoading(true);
try {
const res=await fetch("/api/auth/login",{
method:"POST",
headers:{"Content-Type":"application/json"},
body:JSON.stringify({userName,password})
})
const data=await res.json();
if(data.error){
throw new Error(data.error);
}
localStorage.setItem('chat-user',JSON.stringify(data));
setAuthUser(data);
} catch (error) {
toast.error(error.message);
}finally{
setLoading(false);
}
}
return {loading,login};
}
export default useLogin;
function handleInputErrors({userName,password}){
if(!userName||!password){
toast.error('Please fill all fields');
return false;
}
if(password.length<6){
toast.error("Password must be atleast 6 characters")
return false;
}
return true;
}
this is my userLogin hook here I stored login details of users . I am expecting after login from different tabs user data in localstorage should be different in different tab for all users who loged in.
Khushboo Kachhi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.