I use the below code but it does not work.

When I entered to homepage the log is not display as I want (In console 
log, it show : *first* then *last*, and the last is *second*) so It always 
return token invalid because it does not run in order. I dont know where I 
wrong.
Please help me. Thank you all !

  logIn(identifi : string, password : string ) {
    let headers = new Headers();
    headers.append('Access-Control-Allow-Origin', '*');
    headers.append('Access-Control-Allow-Methods', '*');
    headers.append('Access-Control-Allow-Headers', 'Content-Type, 
Access-Control-Allow-Headers, Authorization, X-Requested-With');    
    let body = {
      "user_id" : identifi,
      "password" : password
    };
    let options = new RequestOptions({ headers: headers });
    return this.http.post(api.logIn, body, options).map((res:Response) => 
res.json());
  }  

  checkToken() {
    this.storage.get('user').then((user) => {
      if(user) {
        this.token = user.token;
        this.token_date = user.token_date - 100;
        this.currentDate = new Date();
        this.expDate = new Date(this.token_date * 1000);
        if (this.expDate === this.currentDate) {
          // token expired, login again     
          console.log('first');  
          this.logIn(user.user_id, user.password).subscribe(
            data => {
              this.storage.set("user", userInfo);  
              console.log('second');
              return  userInfo.token;
            },
            err => {
              console.log(err);
            });
        }
      }
    })
  }

  getDataHomepage(retailerId: number,pageIndex: number) {
    this.checkToken();
    console.log('last');
    let headers = new Headers();
    headers.append('Authorization', 'Bearer ' + this.token);
    let options = new RequestOptions({ headers: headers });
    return this.http.get(api.promotion + '?retailer_id=' + retailerId + 
'&page=' + pageIndex, options).map((res:Response) => res.json());
  }

-- 
You received this message because you are subscribed to the Google Groups 
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to angular+unsubscr...@googlegroups.com.
To post to this group, send email to angular@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to