title: How can we add another subscribe action in the http observable in angularjs2?
body: <p>I am new to Angular2. I have a LoginComponent which takes in an action called submitLogin from a submit button. When clicked, it will send the username and password (via the UserService) to the API server (with the help of UtilService). If the server responds OK, it will return with a token, and we would save it locally and redirect it.</p>
  
  <p>So far, I have managed to send the username and password and save the token. But, I am confused on how we can add another action in the LoginComponent so that we can redirect the user. (I have added a comment in the login.component.ts below to clarify what I would like to do).</p>
  
  <p>Feel free to let me know if I am not making myself clear.</p>
  
  <p>Here are the codes that I have:</p>
  
  <p>login.component.ts has the following:</p>
  
  <pre><code>import {Component, OnInit} from 'angular2/core';
  import {NgForm}    from 'angular2/common';
  import {UserService} from '../services/user.service';
  
  @Component({
    selector: 'login',
    templateUrl: './components/login/login.component.html',
    styleUrls: ['./components/login/login.component.css']
  })
  export class LoginComponent implements OnInit {
    public username: string;
    public password: string;
    public loginResult: string;
  
    constructor(private _userService: UserService) {}
    ngOnInit() { }
    submitLogin() {
      var p = this._userService.login(this.username, this.password);
      // HOW TO: 
      // I know p is an observable. But, is it possible to add a condition as follow:
      // if p is success, then redirect
      // if p failed, then return
    }
  }
  </code></pre>
  
  <p>user.service.ts:</p>
  
  <pre><code>import {Injectable} from 'angular2/core';
  import {UtilsService} from './utils.service';
  
  @Injectable()
  
  export class UserService {
    constructor(private _utils: UtilsService) { }
  
    login(username: string, password: string) {
      var request = {
        method: 'POST',
        headers: { 'Authorization': 'Basic ' + btoa(username + ':' + password) },
        url: '/auth/',
        data: null
      };
  
      return this._utils.api(request).subscribe(
        data =&gt; this.setAccessToken(data.json().access_token),
        err =&gt; console.log(err)
      );
      var promise = this._utils.api(request);
      promise.then((response) =&gt; {
        this.setAccessToken(response.token);
      });
    }
  
  }
  </code></pre>
  
  <p>utils.service.ts:</p>
  
  <pre><code>import {Injectable} from 'angular2/core';
  import {Http, Headers} from 'angular2/http';
  
  @Injectable()
  
  export class UtilsService {
    private apiUrl: string;
    constructor(public http: Http) {
      this.apiUrl = '/api/1';
    }
    api(request: any) {
      var headers = new Headers();
      var url = this.apiUrl + request.url;
  
      if(request.headers) {
        for(var key in request.headers) {
          headers.append(key, request.headers[key]);
        }
      }
  
      if(!request.type || request.type === 'GET') {
        return this.http.get(url, { headers: headers });
      }
  
      if(request.type === 'POST') {
        return this.http.post(url, request.data, { headers: headers });
      }
  
      if(request.type === 'PUT') {
        return this.http.put(url, request.data, { headers: headers });
      }
  
      if(request.type === 'DELETE') {
        return this.http.delete(url, { headers: headers });
      }
      return false;
  
    }
  }
  </code></pre>