0

I am new to Angular & have written below code using in-memory-web-api for Login POC,

DB-service.service:

import { InMemoryDbService } from 'angular-in-memory-web-api';

export class InMemoryDataService implements InMemoryDbService {
  createDb() {
      let users = [
          {id: 1, username: 'user1', password:'user1', name:'John'},
          {id: 2, username: 'user2', password:'user2', name:'David'},
          {id: 3, username: 'user3', password:'user3', name:'Brad'},
          {id: 4, username: 'user4', password:'user4', name:'Jim'},
          {id: 5, username: 'user5', password:'user5', name:'Saun'}
      ];

      return {users};
  }
}

user.service:

import { loginUser } from '../_model/user';

@Injectable()

export class UserService {

  private headers = new Headers({ 'Content-Type': 'application/json' });
  private userUrl = 'api/users';  // URL to web api

  constructor(private http:  Http) {
  }


  getLogin(username: string, password: string) {
    const url = `${this.userUrl}`;    
    return this.http.get(url).map(res => res.json());
  }
}

model/user.ts

export class loginUser {
    constructor(
        public id: number,
        public username: string,
        public password: string,
        public name: string) { }
}

login.component.ts

login(form: FormGroup) {

    if (this.loginForm.valid) {
      this.userData.getLogin(form.value.loginUserTxt, form.value.loginPassTxt)
      .subscribe(
        data => {              
          console.log(data);  
           //comparing the data from the array in DB-service.service  
        },
        error => {              
          console.log("Fail");
        });

    }

My issue is that how can I compare that data in subscribe of login.component.ts with the input value.

1 Answer 1

1

I have update the code below in subscribe

this.loggedUser = this.userData.getAuthenticate(data, form.value.loginUserTxt, form.value.loginPassTxt)    
 if (this.loggedUser == "Invalid") {
      //error
 } else {
    //redirect
  }

Thanks all

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.