Advertisement
Guest User

Untitled

a guest
Apr 28th, 2016
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. Error:(27, 16) TS2322: Type 'Observable<void>' is not assignable to type 'Observable<User>'
  2. Type 'void' is not assignable to type 'User'.
  3.  
  4. export class User {
  5. constructor(
  6. public id:number,
  7. public name:string,
  8. public username:string,
  9. public password:string
  10. ) { }
  11. }
  12.  
  13. import {Injectable} from 'angular2/core';
  14. import {Headers, RequestOptions} from 'angular2/http';
  15. import {Observable} from "rxjs/Observable";
  16. import {User} from "./../user";
  17. import {Http, Response} from 'angular2/http';
  18.  
  19. @Injectable()
  20. export class LoginService {
  21.  
  22. constructor (private http: Http) {}
  23.  
  24. private _user_session_url = 'auth/sign_in';
  25.  
  26. token:string;
  27. expiry:string;
  28. client:string;
  29. uid:string;
  30.  
  31. login (username: string, password: string) : Observable<User> {
  32. let body = JSON.stringify({ username: username, password: password });
  33. let headers = new Headers({
  34. 'Content-Type': 'application/json',
  35. 'accept': 'json'
  36. });
  37. let options = new RequestOptions({ headers: headers});
  38.  
  39. return this.http.post(this._user_session_url, body, options).map(this.extractData).catch(this.handleError)
  40. }
  41.  
  42. private extractData(res: Response) {
  43. if (res.status < 200 || res.status >= 300) {
  44. throw new Error('Bad response status: ' + res.status);
  45. }
  46.  
  47. let data = res.json();
  48. console.log(data);
  49. this.token = data.token;
  50. this.expiry = data.expiry;
  51. this.client = data.client;
  52. localStorage.setItem('access-token', this.token);
  53. localStorage.setItem('client', this.client);
  54. localStorage.setItem('expiry', this.expiry);
  55. localStorage.setItem('uid', this.uid);
  56. }
  57.  
  58. private handleError (error: any) {
  59. let errMsg = error.json().error || 'Server error';
  60. console.error(errMsg); // log to console instead
  61. return Observable.throw(errMsg);
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement