4. login.service
imports
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Rx';
import { User } from './user.model';
@Injectable()
export class LoginService {
private _endpointUrl: string = "http://127.0.0.1:8080/";
constructor(private _http: Http) {
}
login(user: any): Observable<string> {
var headers = new Headers();
headers.append('Content-Type', 'application/json');
let body = JSON.stringify(user);
let msg = this._http
.post(this._endpointUrl + "user/login", body, { headers: headers })
.map(
res => {
if (res.status == 202) {
console.log("200 " + res.json() || {});
console.log('+++++++++++++++++++++++++++ '+res.json().userName);
return res.json().userName;
} else{
console.log("406 " + res.json);
return "Login Denied";
}
}
)
.catch(handleError);
return msg;
}
}
function handleError(error: any) {
// log error
// could be something more sofisticated
console.log("handling the error");
let errorMsg = error.message || `Yikes! There was was a problem with our hyperdrive device and we couldn't retrieve your data!`;
console.log(errorMsg);
console.error(errorMsg);
// throw an application level error
return Observable.throw(errorMsg);
}
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Rx';
import { User } from './user.model';
@Injectable()
export class LoginService {
private _endpointUrl: string = "http://127.0.0.1:8080/";
constructor(private _http: Http) {
}
login(user: any): Observable<string> {
var headers = new Headers();
headers.append('Content-Type', 'application/json');
let body = JSON.stringify(user);
let msg = this._http
.post(this._endpointUrl + "user/login", body, { headers: headers })
.map(
res => {
if (res.status == 202) {
console.log("200 " + res.json() || {});
console.log('+++++++++++++++++++++++++++ '+res.json().userName);
return res.json().userName;
} else{
console.log("406 " + res.json);
return "Login Denied";
}
}
)
.catch(handleError);
return msg;
}
}
function handleError(error: any) {
// log error
// could be something more sofisticated
console.log("handling the error");
let errorMsg = error.message || `Yikes! There was was a problem with our hyperdrive device and we couldn't retrieve your data!`;
console.log(errorMsg);
console.error(errorMsg);
// throw an application level error
return Observable.throw(errorMsg);
}
Comments
Post a Comment