For loop login Authentication Issue

Hello,after 2 days of searching and brainstorming i found out how to make an login authentication. I created an array from a JSON which has data on it. Then i made the below code which compares input data for example:Code,Mobile with the values stored in the array. If the authentication is ok prints “OK” and if not “error”.
The Issue here is that when its Ok it prints both OK and Error .
I think that this issue is because of looping…i mean because it does a searching in the array and cant find the value so in the IF_Statement is false so it prints “Error”
How can i fix that? my syntax? Maybe i need something like "Do loop until you find that this.x==this.a[i].Need help thanks.

here is my code below

 authenticationUser(){

    for(let i=0; i<this.loginservice.users.length;i++){
      if(this.inputCode==this.loginservice.users[i]|| this.inputMobile==this.loginservice.users[i]){

        console.log(" Credentials ok");

      }else {
        console.log(" Error Try Again ")
      }}
  }

This concept is a bit…absurd, but, I can help you with the problem:

const foundUser = this.loginService.users.find(user => user === this.inputCode || user === this.inputMobile);
const message = foundUser ? 'Credentials Ok' : 'Error try again';
console.log(message);
1 Like

It works thank you!

different approach
how did you figured out?

It’s the same thing, just done in a way specific to what you want, rather than a loop.

A couple basic tips I can give you that I applied:

So rather than nesting an if block inside a for loop, I solved the problem one step at a time. First I tried to find a user. Since you were trying to find a user, I used Array.find instead of just a loop, since finding something was your true goal. The find will either return a user if it can find one, or undefined if it can’t. That’s step one broken out into it’s own flat step. Then I simply check if the user was found or not and create your message from that.

1 Like

yes i didnt knew any of that Array.(tools) like find etc . Only the push,pull
Thanks a lot mate pretty much identical answer !
you unstucked me really fast!

@rlouie is much more diplomatic than I am in saying “a bit…absurd”. This entire concept is meaningless. It doesn’t authenticate anything. It provides no security whatsoever. There is no point to any of it.

Submit authentication credentials (over an encrypted connection) to a server that stores verification information in a properly hashed manner, such as bcrypt or scrypt. Have that server then return a JWT, which can be appended to subsequent requests requiring authentication.

No matter how loginservice.users is populated, whether burned into the app binary or retrieved over HTTP, anybody with a copy of the app has complete access to it, and can trivially pass as any user.

1 Like

Thanks for the info rapropos but
i am newbie and i don’t know that much as you do
I dont know about encrypted connections etc…is there any documentation that i have to read?
Also the Http documentation of angular that you send me last time in order to get rid of timeouts was very general…

I use also timeouts because the wait response is forever…
I dont have any resources to read about HTTP response,security etc
if there is something please send a link…

I am trying my best i study programming since november and i am working on a mobile project all alone …
because in this company i am the only developer…