I am trying to build a mobile client for an existing web application using Ionic 3/Angular 4.3.
We are using cookies for sessions and CSRF protection. I have implemented the following HttpInterceptor
in order to attach and send token cookies with HTTP requests:
import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http';
import { Observable } from "rxjs";
import { CookieService } from 'ngx-cookie';
import 'rxjs/add/operator/map';
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(private cookies: CookieService) {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
var authReq = this.cookies.get('XSRF-TOKEN') ? request.clone({
withCredentials: true,
headers: request.headers.set('X-XSRF-TOKEN', this.cookies.get('XSRF-TOKEN'))
}) : request.clone({ withCredentials: true });
return next.handle(authReq);
}
}
Testing from the browser works fine as long as I run with ionic serve address=localhost
(address flag needed to bypass CORS).
However, cookies do not seem to be sent or persisted when testing from an Android device using ionic cordova run android
.
As you can see from above, I have set withCredentials
to true
as most answers to similar posts suggest.