0

I have a single user login system. In the entire org and in all application credential is same. When I am sending login request from angular getting below response :

"Status Code{\"statusCode\":200,\"body\":\"\",\"headers\":{\"date\":\"Thu, 13 Sep 2018 16:04:46 GMT\",\"x-powered-by\":\"Servlet/3.0\",\"content-length\":\"0\",\"set-cookie\":[\"LtpaToken2=UXVOlmR+cut/Qu/cxxxmWaEOEkCtxxxq22GgCI4Iz8nffc1n8O1l5AE0sUCPQfZ+nB9vXS/WMkzxanOexJHzJZb4IgqoxTc0UztF+1amzVysdvdBSSIoblF8fFc9iRMkjYPbxZISg29sU1Q6Au0+Dh50ARY1NhkRQ8iwVst1X6Pbc3Dn4YpLO08utqAi9+35Pun+IFJnbTF6BRvleLKXDifZzwmuZlRKs5gseb9GELQYjTLlXFykm8aq0Ulh0Z8VT/xi0uRFFiR0eUrEvo58sev/3XPYv10//ZTkWOqFnADdzhsBoW4wtWVjlFcvlB/24TBaKtNWmYJd3/tsnoRCWPn1yd4udC0YDFsr3h5SGAjgCRMg7gH8+hvpnIiKzzfcxNmQTPlRjav+SUkBfIxAQ5gmGOmDXDcuzMSbx8zJk1RGNcM/Ksmrcv+SOoDMtmCDHWWqTVn9VLPAnF9bZE7Fkmis7QcNxK5UAoMTCCzgfwMEwHeWm4fY7DblWi8RM=; Path=/\",\"JSESSIONID=0000LsRRstmAD-Y923str-8:-1; Path=/\"],\"expires\":\"Thu, 01 Dec 1994 16:00:00 GMT\",\"cache-control\":\"no-cache=\\"set-cookie, set-cookie2\\"\",\"content-language\":\"en-US\",\"connection\":\"close\"},\"request\":{\"uri\":{\"protocol\":\"http:\",\"slashes\":true,\"auth\":null,\"host\":\"abcd.com\",\"port\":80,\"hostname\":\"abcd.com\",\"hash\":null,\"search\":null,\"query\":null,\"pathname\":\"/hello/rest/login\",\"path\":\"/hello/rest/login\",\"href\":\"http://abcd.com\"},\"method\":\"GET\",\"headers\":{\"authorization\":\"Basic r8htMGw5adkhobSMzYXVn\"}}}"

I am getting undefined when I am trying to access statuscode in my login component,

this.apiDataService.getLogin(user).subscribe((data: any) => {
      if (data.statusCode==200) {
        this.apiDataService.storeUserData(data.headers, data.authorization);
        this.router.navigateByUrl('/validate');
      } else {
        this.loginError = true;
        this.errMsg = 'Invalid username/password';
      }

please suggest me how to get statuscode and what should I use for session management, like set-cookie i am not able to use. });

etarhan
  • 4,138
  • 2
  • 15
  • 29
SUBHASIS MONDAL
  • 705
  • 9
  • 20
  • please edit your question and put a readable response of getLogin api – Fartab Sep 14 '18 at 09:23
  • 1
    because you are receiving your response as string, you need to parse it, may be with JSON.parse – Fateh Mohamed Sep 14 '18 at 09:24
  • If you are using `HttpClient` then - by default - in response you have access only to the response body. If you want to access whole response object check this solution https://stackoverflow.com/a/46809000/8531463 – Karol Trybulec Sep 14 '18 at 09:42
  • There is a parse erron in the piece where there is "cache-control":"no-cache=" set - cookie, ... may be you miss a comma – Gianluca Conte Sep 14 '18 at 09:48

2 Answers2

2

Try data.json() to parse the response as JSON object,

this.apiDataService.getLogin(user).subscribe((data: any) => {
  var response=data.json();
  if (response.statusCode==200) {
    this.apiDataService.storeUserData(response.headers, response.authorization);
    this.router.navigateByUrl('/validate');
  } else {
    this.loginError = true;
    this.errMsg = 'Invalid username/password';
  }
Yuvaranjani
  • 300
  • 2
  • 12
1

Try this:

this.apiDataService.getLogin(user).map((res: any) => res.json()).subscribe((data: any) => {
      if (data.statusCode==200) {
          this.apiDataService.storeUserData(data.headers, data.authorization);
          this.router.navigateByUrl('/validate');
      } else {
          this.loginError = true;
          this.errMsg = 'Invalid username/password';
      }
Ritesh Waghela
  • 3,474
  • 2
  • 19
  • 25
  • I tried this one but getting below error, trying to fix it TypeError: res.json is not a function. But I found this one: https://stackoverflow.com/questions/46630893/angular-res-json-is-not-a-function – SUBHASIS MONDAL Sep 14 '18 at 10:17
  • Are you using httpClient or http? Can you please post the code of getLogin funciton? – Ritesh Waghela Sep 14 '18 at 10:20
  • I am using httpclient. import { HttpClient,HttpHeaders} from '@angular/common/http'; getLogin(user){ let headers = new HttpHeaders() headers = headers.append('Content-Type', 'application/json'); console.log("Inside Get Login Details service"); return this.http.post('http://localhost:3000/app/login', user,{headers}); } – SUBHASIS MONDAL Sep 14 '18 at 10:36
  • 1
    I have fixed the issue as I modified some changes in server side so I get json, i was getting string value. That caused the issue. Thank you sir for your help. – SUBHASIS MONDAL Sep 14 '18 at 10:57