The Official Ionic Blog

Build amazing native and progressive web apps with HTML5

Single Page Apps are ruling the world and AngularJS is leading the charge. But many of the lessons we learned in the Web 2.0 era no longer apply, and few are as drastically different as authentication.

Depending on where you build and release your AngularJS app, there are different ways to handle authentication and I hope this post clears some of that up.

CORS

CORS is an oft-misunderstood feature of new browsers that is configured by a remote server. CORS stands for Cross-Origin-Resource-Sharing, and was designed to make it possible to access services outside of the current origin (or domain) of the current page.

Like many browser features, CORS works because we all agree that it works. So all major browsers like Chrome, Firefox, and IE support and enforce it. By using these browsers, you benefit from the security of CORS.

That means certain browsers do not enforce it, so it is not relevant there. One large example is a native Web View for things like Cordova and Phonegap (and hence, Ionic apps). However, these tools often have configuration options for whitelisting domains so you can add some security that way.

Configuring CORS on the Server

The way CORS works is the server decides which domains it will accept as clients. This means an open API like Twitter might allow any clients, or a closed API might decide to only allow access from the domain of the running client app.

I won’t get into the details of configuring CORS on the server side, but it’s really just setting some headers. Here’s how you might do it in nginx.

There is one header in particular you must have if you want to do session based authentication in a single page app: Access-Control-Allow-Credentials: true which we show next.

AngularJS Auth

If you use the standard $http service to access remote APIs, it will Just Work as long as the server is configured to allow requests from your domain and you don’t need to store cookies.

But for many applications, we also need to set and store cookie information for things like logins. By default this is not allowed in most browsers and you’ll be smashing your head wondering why the cookie information isn’t being saved!

Enter: withCredentials. withCredentials is a flag set on a low-level XMLHttpRequest (AJAX) object, but in Angular we can configure our $http requests to set this flag for everything by doing:

angular.module('myApp')

.config(['$httpProvider', function($httpProvider) {
  $httpProvider.defaults.withCredentials = true;
}])

CSRF

Many servers use CSRF as a security feature and you can certainly keep this feature in a hybrid app if you wish. CSRF is a way to ensure the client making a request is the same one that the server expects to make the request. This keeps someone from sniffing your cookie session data and making requests pretending to be you (and changing your password, for example).

To make for CSRF, we can tell $http to set the correct header for CSRF (might depend on your server framework, this one is for Django) using the specific cookie name:

angular.module('myApp', ['ngCookies'])

.run(['$http', '$cookies', function($http, $cookies) {
  $http.defaults.headers.post['X-CSRFToken'] = $cookies.csrftoken;
}]);

While I’ve found this to work pretty well, if the CSRF token changes mid-session (for example if a new user signs up) the token won’t update. To fix this, we can use an HTTP Interceptor to always set the correct CSRF header value before each request:

angular.module('myApp')

.provider('myCSRF',[function(){
  var headerName = 'X-CSRFToken';
  var cookieName = 'csrftoken';
  var allowedMethods = ['GET'];

  this.setHeaderName = function(n) {
    headerName = n;
  }
  this.setCookieName = function(n) {
    cookieName = n;
  }
  this.setAllowedMethods = function(n) {
    allowedMethods = n;
  }
  this.$get = ['$cookies', function($cookies){
    return {
      'request': function(config) {
        if(allowedMethods.indexOf(config.method) === -1) {
          // do something on success
          config.headers[headerName] = $cookies[cookieName];
        }
        return config;
      }
    }
  }];
}]).config(function($httpProvider) {
  $httpProvider.interceptors.push('myCSRF');
});

This will set the CSRF request header to the current value of the CSRF cookie for any request type not in allowedMethods.

Credentials and CORS

One thing to note when using withCredentials: true in your app and configuring the server for CORS is that you may not have your Access-Control-Allow-Origin header set to '*'. It must be configured to a few select origins. If you absolutely must have this set to *, then I suggest doing something beyond cookie based authentication, such as token-based authentication.

Token Auth

The methods described above work for cookie-based authentication that is common in most server-side setups. However, some APIs expect HTTP Basic Authentication or use a token-based system.

While the correct use of CORS will avoid cross-domain pitfalls of cookie-based authentication, those methods may be a better fit for your use case. In that case, take a look at this great post on token authentication with AngularJS.

We may revisit this topic in the future to add our thoughts on Token-based authentication.

  • C.R.B.

    Hi, if i cant use wildcard in Access-Control-Allow-Origin what i need recive here if i install my app on mobile ?

  • Antonia Hulha

    For more info, read this article: http://blog.ionic.io/handling-cors-issues-in-ionic/

  • http://devdactic.com/ Simon Reimler

    For those who a re looking for a Token-based solution: I made a detailed guide on how to setup user Authentication inside Ionic with AngularJS: http://devdactic.com/user-auth-angularjs-ionic/

  • Leon Sasson

    Where is `withCredentials` storing the “cookies”? I’m having some of my users spontaneously log out from the app and I believe this could be the root cause.

    • Fredster

      Did you find out where the cookes are stored? I’m wondering the same thing.

  • Shai Snir

    This method works when using browser but when using iOS simulator, I can login and get all the services from the server but once I close the app, the session expires and I get thrown back to login page of my app…
    This behaviour only occurs on IOS and not on any browser…

    • Bartosz BiaƂas

      Hey, did you find a solution for your problem?

  • Trevor Huey

    For people having problems with the session expiring when closing the app, see my post here: http://forum.ionicframework.com/t/cookies-disappear-on-app-restart/26213. My problem was that the cookie didn’t have an expiry set, meaning the browser cleared it on close.

  • Priya R

    Angularjs is the great javascript framework that has some compelling features not for developers but also for the designers. Angularjs is very essential for the web developers to know about its importance.

    http://www.angularjstraining.in/

  • Yur Gasparyan

    Please help! your csrf token code is not working

  • Ravindra Desai

    Thank you. It worked and saved my lot of time

  • Anchit Jindal

    hi how can i remove cookies from request header set by

    $http.defaults.headers.post[‘X-CSRFToken’] = $cookies.csrftoken; In header it show as
    Cookie
    JSESSIONID=F215EF9C41E8D897F103751DE8EC833C; AWSELB=8BE3A51B068B8505EFF78592B3B122EBDC441C205CA70B58

  • mahesh

    Can you please provide example code

  • Maxime Duc

    How it’s works with ionic2 ?

    Thx