# Cookie-based Authentication for Ionic 2 / Angular 2

**URL:** <https://forum.ionicframework.com/t/cookie-based-authentication-for-ionic-2-angular-2/55495>\
**Category:** ionic-v3\
**Created:** [June 22, 2016, 10:36am UTC](https://forum.ionicframework.com/t/cookie-based-authentication-for-ionic-2-angular-2/55495 "2016-06-22T10:36:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamsamhk](https://avatars.discourse-cdn.com/v4/letter/i/ebca7d/32.png) [@iamsamhk](https://forum.ionicframework.com/u/iamsamhk)\
**Post date:** [June 22, 2016, 10:36am UTC](https://forum.ionicframework.com/t/cookie-based-authentication-for-ionic-2-angular-2/55495/1 "2016-06-22T10:36:59Z")

</div>

For Ionic 1, we are following this very useful tutorial  
[http://blog.ionic.io/angularjs-authentication/](http://blog.ionic.io/angularjs-authentication/)

However seems that no longer works in angular 2

Can anyone suggest if there’s any similar tutorial for ionic 2 / angular 2 ?

I can see this in stackoverflow, however no idea how to intergrate into ionic 2

> <https://stackoverflow.com/questions/34494876/what-is-the-right-way-to-use-angular2-http-requests-with-django-csrf-protection>

Thank you.

---

<div class="post-metadata">

**Author:** ![gaurav](https://avatars.discourse-cdn.com/v4/letter/g/b487fb/32.png) [@gaurav](https://forum.ionicframework.com/u/gaurav)\
**Post date:** [August 31, 2016, 4:56pm UTC](https://forum.ionicframework.com/t/cookie-based-authentication-for-ionic-2-angular-2/55495/2 "2016-08-31T16:56:36Z")

</div>

@iamsamhk any updates you found working with cookie based authentication or any online resources ?

---

<div class="post-metadata">

**Author:** ![paladini](https://avatars.discourse-cdn.com/v4/letter/p/e5b9ba/32.png) [@paladini](https://forum.ionicframework.com/u/paladini)\
**Post date:** [December 21, 2016, 1:55pm UTC](https://forum.ionicframework.com/t/cookie-based-authentication-for-ionic-2-angular-2/55495/3 "2016-12-21T13:55:02Z")

</div>

Any updates on this matter? Someone here was able to make cookie-based authentication using Ionic 2?

---

<div class="post-metadata">

**Author:** ![Barylskyi](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@Barylskyi](https://forum.ionicframework.com/u/Barylskyi)\
**Post date:** [January 28, 2017, 1:44pm UTC](https://forum.ionicframework.com/t/cookie-based-authentication-for-ionic-2-angular-2/55495/4 "2017-01-28T13:44:16Z")

</div>

I think what you need is setting `withCredentials = true` on Http requests which you need to use cookies with.  
[this](http://stackoverflow.com/questions/38615205/angular-2-http-withcredentials) link might also help.

In Ionic 1 this was done using `$httpProvider.defaults.withCredentials = true;`.  
If you want to append `withCredentials = true` to every request in Ionic 2 app - you could create an Http interceptor using [this](https://github.com/voliva/angular2-interceptors) plugin and create such a class:

```auto
/**
 * Appends the auth cookie to all Http requests
 **/

import { Interceptor, InterceptedRequest, InterceptedResponse } from 'ng2-interceptors';
import { Injectable } from "@angular/core";

@Injectable()
export class CookieAppender implements Interceptor {
  public interceptBefore(request: InterceptedRequest): InterceptedRequest {
    request.options.withCredentials = true;
    return request;
  }

  public interceptAfter(response: InterceptedResponse): InterceptedResponse {return response}
}

```

Hope this helps someone.

---

<div class="post-metadata">

**Author:** ![bhlokhandwala](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bhlokhandwala/32/116969_2.png) [@bhlokhandwala](https://forum.ionicframework.com/u/bhlokhandwala)\
**Post date:** [March 23, 2018, 11:33am UTC](https://forum.ionicframework.com/t/cookie-based-authentication-for-ionic-2-angular-2/55495/5 "2018-03-23T11:33:24Z")

</div>

this doesn’t work on ios … the cookie doesn’t persist once you kill the app. does anyone what is the solution
