2

This is going to be a long question.

For windows authentication to work with angular I have wrapper for the http calls as shown below

import { Injectable } from '@angular/core';
import { Http, Response, RequestOptions, Headers, RequestOptionsArgs } from '@angular/http';
import { Config } from '../_helpers/config';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class HTTPUtility {
    public baseApiUrl: string;
    constructor(private http: Http, private config: Config) {
        this.baseApiUrl = this.config.getByKey('baseApiUrl') || '';
    }

    public getApiUrl(url) {
        return this.baseApiUrl + url;
    }

    public get(url: string, options?: RequestOptions) {
        if (!options) {
            options = this.getDefaultHeaders();
        }
        return this.http.get(this.getApiUrl(url), options)
            .catch(this.handleError);
    }

    private getDefaultHeaders() {
        let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
        headers.append('Accept', 'application/json');
        return new RequestOptions({ headers, withCredentials: true });
    }

    public handleError(response: Response) {
        return Observable.throw(JSON.parse(response.json().Message) || 'Server error');
    }
}

If you observe new RequestOptions({ headers, withCredentials: true }); is allowing browser to send credentials to server for windows authentication. And it's working great for everything.

Now coming to the issue, I have sampleComponent in which i'm using ServerDataSource as shown below:

import { Component, OnInit, NgZone } from '@angular/core';
import { Http, RequestOptions } from '@angular/http';
import { Ng2SmartTableModule, ServerDataSource } from 'ng2-smart-table';

@Component({
selector: 'criteria',
templateUrl: './criteria.component.html',
styles: [require('./criteria.scss')],
})

export class SampleComponent implements OnInit {

    Source: ServerDataSource;
    settings: any;
    constructor(
    private http: Http) {
    this.Source = new ServerDataSource(http, { endPoint: 'https://xxxxxx.org/yyy/GetCriterias'});
   }

     ngOnInit(): void {
        // this.settings = {}// assigning the settings.
    }
}

As you can see ServerDataSource is accepting Http instance and I have checked there documentation and haven't found any way to pass to RequestOptions. So the web api call made by ng2-smart-table fails with 401 status as credentials is not passed.

To resolve this issue I have made changes directly to ng2-smart-table source file to be specific 'server.data-source.js' and here is the change

ServerDataSource.prototype.createRequestOptions = function () {
        var requestOptions = {withCredentials : true}; // this where I have added the withCredntials flag
        requestOptions.params = new URLSearchParams();
        requestOptions = this.addSortRequestOptions(requestOptions);
        requestOptions = this.addFilterRequestOptions(requestOptions);
        return this.addPagerRequestOptions(requestOptions);
    };

With this change everything is working fine as of now. But I could have issue in future, if we upgrade the package in that case I have to again make changes.

So if any one can help me to fix the issue in some other way please let me know.

Links: https://github.com/akveo/ng2-smart-table/blob/master/src/app/pages/examples/server/advanced-example-server.component.ts

Thanks.

Rajeev Ranjan
  • 497
  • 4
  • 16

0 Answers0