how to validate user name and password from given array of objects?

hello i have a hook in app.js:

 const[customer,setCustomer]=useState([
    {name:'aaaaaa', password:'11111', balance:78000},
    {name:'bbbbbb', password:'22222', balance:5000},
    {name:'cccccc', password:'33333', balance:15000},
    {name:'dddddd', password:'44444', balance:1000000},
  ]);

i sent this hook through Route to a sing in component.
how can i validate each customer to his own password?

inside the component i made 2 inputs that sent the value that written to 2 hooks and a button, i wand the validation to happen when im clicking on the button (onClick). and return the index of the object that holds ths given name adn password so i can pass it to the customer page/component.
thank you.

singin component:

const[name,setName]=useState('');
const[password,setPassword]=useState('');



<input onChange={(e)=>{setName(e.target.value)}} placeholder='user name'></input>
<inputonChange={(e)=>{setPassword(e.target.value)}} placeholder='password'></input>
<button onClick={valid}>Enter</button>```

>Solution :

Just find with the set values if it exists or not in your array

const valid () => {
  let existsUser = customer.find((x) => (x.name === name && x.password === password))
  if(existsUser){
    alert('Navigate to new page')
  }
}

Leave a Reply