POST error : 401 Unauthorized, authentication


#1

Hi,

I have a problem authenticating a user. In case of problems in the credentials (email and password), I display an error message with a toast. But since this morning, I have the error 401 Unauthorized that appears and so the toast no longer displays error messages. I do not understand where it comes from.

The console’s error


POST UrlAPI 401 (Unauthorized)
EXCEPTION: Response with status: 401 Unauthorized for URL: UrlAPI

And this is my code for the POST request

return this.http.post(link, data, options)
    .map((res: Response) => res.json())
    .subscribe((response) => {
            console.log("RESPONSE: ", response);
            if (response.status === 'ok'){
              this.UserData.setSessionId(response.session_id);
              console.log("SESS_ID SAVED", this.UserData.getSessionId());
              this.nav.setRoot(TabsPage);
             }
            else {
              let toast = this.toastCtrl.create( {
                  message: response.errors,
                  duration: 3000,
                  position: 'top'
              });
            toast.present();
          }
        });

Reponse Headers

Access-Control-Allow-Origin:*
Cache-Control:no-cache
Connection:Keep-Alive
Content-Length:57
Content-Type:application/json
Date:Tue, 23 May 2017 13:49:56 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.4.10 (Debian)

Request Headers

Accept:*/*
Accept-Encoding:gzip, deflate, br
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:16
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Host: UrlAPI
Origin:http://localhost:8101
Referer:http://localhost:8101/?ionicplatform=ios&ionicstatusbarpadding=true
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36

#2

What are you intending to return from the “code for the POST request”?


#3

The API response, like that

{
  "status": "nok",
  "errors": [
    "Email or password incorrect"
  ]
}

On the console, the message is sent but it is blocked by error 401 and the toast is not displayed…


#4

That’s impossible, because it’s an asynchronous action. What you are currently returning is a subscription.


#5

Yes, I was wrong in my answer. I am a beginner with Angular and Ionic.
An idea to help me?