I have an object array of users, which each object has 'username' and 'password'.
There is then a login page, where once the user enters in a username and a password, I want to pass that into the reducer and compare them with each object in the 'users' array, and if an object that matches both, 'password' and 'username', is found, I want it return 'true'.
Following is the reducer:
const usersReducer = function(users = [], action){
switch (action.type){
case 'VERIFY_USER':
return users.map((user)=> {
if(user.username === action.username && user.password === action.password){
return true
} else{
return false
}
})
}
default:
return users
}
}
But it seems to return true all the time. Am I using map correct? If not, is there such a method where it'll go through each object in an array? Any insight or guidance would be appreciated. Thank you in advance!
EDIT** Calling it from the following:
verifyLoginUser(){
event.preventDefault()
if(this.props.actions.verifyUser(this.props.loginUser.username, this.props.loginUser.password)){
console.log('LOGGED IN')
} else {
console.log('NOT LOGGED IN')
}
}
With the following reducer:
case 'VERIFY_USER':
let isVerified = false;
users.forEach((user)=> {
if(user.username === action.username && user.password === action.password){
isVerified = true;
return false;
}
});
return isVerified;
return users.find(user => user.username === action.username && user.password === action.password)