Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Injectable()
- export class AuthenticationData {
- authenticationEndPoint: string = "url";
- constructor(public http: Http) {
- super();
- }
- //login
- loginUser(username: string, password: string): Observable<any> {
- let headers = new Headers();
- headers.append('content-type', 'application/json');
- let body = '';
- let options = new RequestOptions({ headers: headers });
- let url = this.authenticationEndPoint + encodeURI(username) + '&password=' + encodeURI(password);
- return this.http.post(url, body, options)
- .map(this.extractData)
- .catch(this.handleError);
- }
- //to extract data
- private extractData(res: Response) {
- let body = res.json();
- return body || {};
- }
- //to handle error
- private handleError(error: Response | any) {
- let errMsg: string;
- if (error instanceof Response) {
- const body = error.json() || '';
- const err = body.error || JSON.stringify(body);
- errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
- } else {
- errMsg = error.message ? error.message : error.toString();
- }
- console.error(errMsg);
- return Observable.throw(errMsg);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement