Cookie-based Authentication for Ionic 2 / Angular 2


#1

For Ionic 1, we are following this very useful tutorial
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

Thank you.


#2

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


#3

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


#4

I think what you need is setting withCredentials = true on Http requests which you need to use cookies with.
this 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 plugin and create such a class:

/**
 * 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.


#5

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