How to catch login error in ionic

this.auth.login(‘basic’, details).then( … );

if user entered wrong email and password then how to catch error

this.auth.login(‘basic’, details)
.then(
() => {
this.isUserLoggedIn = true;
this.currentUserData = this.user;
console.log(this.currentUserData);
return this.currentUserData;
}).catch((err: IDetailedError<string>) => {
console.log(err);
});

I cant able to console that error getting
image

1 Like

And without using catch?

this.auth.login('basic', details).then(() => {
    this.isUserLoggedIn = true;
    this.currentUserData = this.user;
    console.log(this.currentUserData);
    return this.currentUserData;
}, (err: IDetailedError) => {
 console.log(err);
});

i am getting same error

sorry I misunderstood then or don’t understand your goal then :frowning:

if user tried to login with wrong password or if that email and password does not exist in my ionic users list then i have to display your login credentials are invalid or user does not exist. But i am not able to get the error response from this.auth.login

When you debug your code, where do the answer goes?

this.auth.login('basic', details).then(() => {
    this.isUserLoggedIn = true; // <------ Debug here (1)
   this.currentUserData = this.user;
   console.log(this.currentUserData);
    return this.currentUserData;
}, (err: IDetailedError) => {
 console.log(err);  // <------ Or debug goes here (2)
});

If (1) and it effectively should not reach a success, then it means that the method this.auth.login does return an error promise as it should.

If (2) I really don’t understand the problem sorry

(2) this one…

i have one user in ionic user database having details
{
“email” : "testing@gmail.com",
“password”: “asdasd”
}

if i pass this details in
this.auth.login(‘basic’, details).then(()=>{
// Here i can able get success result of user
});

but if user entered wrong password then user can’t able to logged in right, i have to show him “your password is invalid”
for this error message i am not able to get the response for invalid credentials, i am getting network error

Actually your error answer contains “Invalid password”. So I guess your server is just giving back a 401 error network error as result when the password isn’t correct.

Error type: 401
Error msg: Invalid password

Yes, that response having answer but request is unauthorised,

I am not using my personal server, Here i am using ionic user service (http://docs.ionic.io/services/users/)

Ok. Sorry seems I can’t help and I loose your time. Hope someone else could help you.

It’s ok not a problem thank you for trying… i will wait for some others to reply…

instead of this
return new Promise(resolve => {
this.auth.login(‘basic’, details)
.then(() => {
this.isUserLoggedIn = true;
this.currentUserData = this.user;
console.log(this.currentUserData);
return this.currentUserData;
},(error: IDetailedError<string[]>)=>{
console.log(error);
});
});

i used this

return new Promise(resolve=>{
let method = "https://api.ionic.io/auth/login"
let headers = new Headers();
headers.append(‘Authorization’, 'Bearer '+ API_TOKEN);
headers.append(‘Content-Type’, ‘application/json’);
let body = JSON.stringify({
‘app_id’: APP_ID,
‘email’: details.email,
‘password’:details.password
});
this.http.post(method,body, { headers: headers })
.map(res => res.json())
.subscribe(data => {
this.currentUserData = data;
resolve(this.currentUserData);
},(error=>{
this.currentUserData = JSON.parse(error._body);
resolve(this.currentUserData);
}));
})

i change code … Now i can able to catch login error… is that right way to code??

@nagarajsabhahith Did you find solution to this ? As, I am facing same problem

1 Like

@Gaurav try this… Below code is working fine for me…

return new Promise(resolve=>{
let method = "https://api.ionic.io/auth/login"
let headers = new Headers();
headers.append(‘Authorization’, 'Bearer '+ API_TOKEN);
headers.append(‘Content-Type’, ‘application/json’);
let body = JSON.stringify({
‘app_id’: APP_ID,
‘email’: details.email,
‘password’:details.password
});
this.http.post(method,body, { headers: headers })
.map(res => res.json())
.subscribe(data => {
this.currentUserData = data;
resolve(this.currentUserData);
},(error=>{
this.currentUserData = JSON.parse(error._body);
resolve(this.currentUserData);
}));
})

Please stop creating needless Promises.

1 Like

@rapropos Would you be able to write a way to catch the error by using less Promises? I’m having the same issue.

anyErrors : any;

in the controller.ts file

this.auth.login('basic', details).then(() => {
    this.isUserLoggedIn = true; // <------ Debug here (1)
   this.currentUserData = this.user;
   console.log(this.currentUserData);
    return this.currentUserData;
}, (err: IDetailedError) => {
this.anyErrors= err; // map the error here
 
});

in the html file

<div >
      <ion-row >
        <ion-item  >
          <ion-label text-wrap color=red color="primary" >
    {{anyErrors}}
    </ion-label>
  </ion-item>
      </ion-row>
    </div>

So any error in ionic .ts will flow the the html page I use this for the login page. i.e below the login submit button, i have the above div code. if there is no error the msg will not display, if there is an error msg it will display.

1 Like