Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm new to Angular , I just write service that check login but I don't have serve side code so I found fake API to test login but I don't know why it doesn't work.

Here's the site of the fake API https://reqres.in/ you can find the login API.

enter image description here


What I have tried:

Here's the service I wrote :-


JavaScript
<pre>import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

@Injectable()
export class AuthService {
  constructor(private http: Http) {
  }

  login(credentials) { 
   return this.http.post('https://reqres.in/api/login', 
      JSON.stringify(credentials)).map(response=>{
        let result = response.json();
         if (result && result.token) {
           localStorage.setItem("token", result.token) ;
           return true;
         }
         else{
           return false;
         }
      });
  }


  isLoggedIn() { 
    return false;
  }
}



Login Component :


JavaScript
<pre>import { AuthService } from './../services/auth.service';
import { Component } from '@angular/core';
import { Router } from "@angular/router";

@Component({
  selector: 'login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent {
  invalidLogin: boolean; 

  constructor(
    private router: Router, 
    private authService: AuthService) { }

  signIn(credentials) {
    this.authService.login(credentials)
      .subscribe(result => { 
        if (result)
          this.router.navigate(['/']);
        else  
          this.invalidLogin = true; 
      });
  }
}
Posted
Updated 11-Sep-18 2:38am
Comments
ZurdoDev 11-Sep-18 8:04am    
Why bother trying to get a fake api to work? How about spend the time writing the code you need instead.
Er. Puneet Goel 11-Sep-18 8:08am    
There are cases we have to use such api's.
Ahmed El Bohoty 11-Sep-18 8:11am    
i just need to confirm that i can make the login system when i integrate with back end team.
Er. Puneet Goel 11-Sep-18 8:07am    
What is the error you are getting ?
Ahmed El Bohoty 11-Sep-18 8:12am    
NO ERRORS BUT THE LOGIN NOT WORK.

1 solution

Please use the following link to learn basic set of login with consuming res api's

Angular 6 Tutorial - Learn Angular 6 in this Crash Course[^]
 
Share this answer
 
Comments
Ahmed El Bohoty 11-Sep-18 8:57am    
Thanks but I already know all of this concepts , If you can help me to find the problem I will be grateful.
Er. Puneet Goel 12-Sep-18 1:43am    
try to look into the console or add debugger. Atleast we need some clue to reach it,
Ahmed El Bohoty 12-Sep-18 7:02am    
I did but not data or bug
Er. Puneet Goel 14-Sep-18 4:03am    
Ahmed..any progress?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900