title: Django session authentication with Angular 2
body: <p>I've been looking all around for session based authentication with Angular 2. </p>
  
  <p>I'm building an application that has Django on backend and Angular 2 on the frontend. To keep the process simple I'm trying to implement Django session authentication.</p>
  
  <pre><code>// Angular 2 authentication service
  import { Injectable } from "@angular/core";
  import { Headers, Http, Response } from "@angular/http";
  
  import "rxjs/add/operator/toPromise";
  import 'rxjs/add/operator/map'
  
  import { AppSettings } from "../../app.settings";
  
  @Injectable()
  export class UserAuthService {
      private headers = new Headers({'Content-Type': 'application/json'});
  
      private loginUrl = `${AppSettings.BACKEND_URL}` + '/api/v1/users/login/';
  
      constructor(
          private http: Http
      ) { }
  
      login(username, password) {
          let data = {
              username: username,
              password: password
          };
          return this.http.post(this.loginUrl, data, this.headers)
              .map((response: Response) =&gt; response.json());
      }
  }
  
  # Django Login view
  def login(self, request):
          username = request.data['username']
          password = request.data['password']
          user = authenticate(username=username, password=password)
          if user is not None:
              login(request, user)
              serializer = self.serializer_class(user)
              return Response(serializer.data, status=status.HTTP_200_OK)
          raise AuthenticationFailed
  </code></pre>
  
  <p>I'm successfully calling backend API and my <code>login</code> view returns the successful response.</p>
  
  <p>Also <code>request.user</code> gets updated after the login but when I try to call the other APIs using Angular or directly browse Django rest API user is not logged in.</p>