# Can't Login through API IONIC 3

**URL:** <https://forum.ionicframework.com/t/cant-login-through-api-ionic-3/126145>\
**Category:** ionic-v3\
**Created:** [March 27, 2018, 10:53pm UTC](https://forum.ionicframework.com/t/cant-login-through-api-ionic-3/126145 "2018-03-27T22:53:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wowkaaps](https://avatars.discourse-cdn.com/v4/letter/w/6bbea6/32.png) [@wowkaaps](https://forum.ionicframework.com/u/wowkaaps)\
**Post date:** [March 27, 2018, 10:53pm UTC](https://forum.ionicframework.com/t/cant-login-through-api-ionic-3/126145/1 "2018-03-27T22:53:27Z")

</div>

Hi there,

I am new to IONIC and I am trying to build a simple application with a login through my API (JWAUTH Laravel).

I was following some tutorials and I can’t understand where my mistake is, when I try to login the code jump right to the error message.

Any ideas why ? Thanks.

AuthServiceProvider

```auto
import { HttpClient, Headers } from '@angular/common/http';

let apiUrl = "http://127.0.0.1:8000/api/";

...

export class AuthServiceProvider {

postData(credentials, type) {
      return new Promise((resolve, reject) => {
        let headers = new Headers();

        this.http.post(apiUrl + type, JSON.stringify(credentials), {headers: headers})
          .subscribe(res => {
            resolve(res.json());
          }, (err) => {
            reject(err);
          });
      });

    }

}

```

Login.ts

```auto
import { AuthServiceProvider } from '../../providers/auth-service/auth-service';

...

export class LoginPage {

responseData : any;
  userData = {"email":"", "password":""};

   constructor(public navCtrl: NavController, public menuCtrl: MenuController, public authServiceProvider : AuthServiceProvider) {
    this.menuCtrl.enable(false, 'myMenu');
  }
  goToHome(params){

    this.authServiceProvider.postData(this.userData, "auth/login").then((result) =>{
      console.log('YES!!!');
      this.responseData = result;
      console.log(this.responseData);
      localStorage.setItem('userData', JSON.stringify(this.responseData));
      this.navCtrl.push(HomePage);
    }, (err) => {
      console.log('erro');
      //Connection failed message
    });
  }

}

```

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [March 27, 2018, 11:03pm UTC](https://forum.ionicframework.com/t/cant-login-through-api-ionic-3/126145/2 "2018-03-27T23:03:13Z")

</div>

Any tutorial that recommends “new Promise()” when dealing with Angular HttpClient is braindead and should not be followed. In addition, you don’t need to be making headers, stringifying anything, or calling `json()` on responses. I would suggest starting over following the idioms documented in [here](https://angular.io/guide/http).

If the problem still persists, with issues like this, it’s important to know where you are running the app and where you are running the server. `127.0.0.1` is where the app is running, so if that’s a device things aren’t going to work unless there’s a proxy running on there.

---

<div class="post-metadata">

**Author:** ![wowkaaps](https://avatars.discourse-cdn.com/v4/letter/w/6bbea6/32.png) [@wowkaaps](https://forum.ionicframework.com/u/wowkaaps)\
**Post date:** [March 27, 2018, 11:14pm UTC](https://forum.ionicframework.com/t/cant-login-through-api-ionic-3/126145/3 "2018-03-27T23:14:59Z")

</div>

Thanks I will take a look.

The app and the server ar both local.

---

<div class="post-metadata">

**Author:** ![daey](https://avatars.discourse-cdn.com/v4/letter/d/c4cdca/32.png) [@daey](https://forum.ionicframework.com/u/daey)\
**Post date:** [September 25, 2018, 8:56am UTC](https://forum.ionicframework.com/t/cant-login-through-api-ionic-3/126145/4 "2018-09-25T08:56:37Z")

</div>

Hi, on postData() try use “resolve(res)” instead of “resolve(res.json())”

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [July 19, 2021, 9:42pm UTC](https://forum.ionicframework.com/t/cant-login-through-api-ionic-3/126145/5 "2021-07-19T21:42:43Z")

</div>

Closed on July 19, 2021, 9:42pm UTC


