Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Error:(27, 16) TS2322: Type 'Observable<void>' is not assignable to type 'Observable<User>'
- Type 'void' is not assignable to type 'User'.
- export class User {
- constructor(
- public id:number,
- public name:string,
- public username:string,
- public password:string
- ) { }
- }
- import {Injectable} from 'angular2/core';
- import {Headers, RequestOptions} from 'angular2/http';
- import {Observable} from "rxjs/Observable";
- import {User} from "./../user";
- import {Http, Response} from 'angular2/http';
- @Injectable()
- export class LoginService {
- constructor (private http: Http) {}
- private _user_session_url = 'auth/sign_in';
- token:string;
- expiry:string;
- client:string;
- uid:string;
- login (username: string, password: string) : Observable<User> {
- let body = JSON.stringify({ username: username, password: password });
- let headers = new Headers({
- 'Content-Type': 'application/json',
- 'accept': 'json'
- });
- let options = new RequestOptions({ headers: headers});
- return this.http.post(this._user_session_url, body, options).map(this.extractData).catch(this.handleError)
- }
- private extractData(res: Response) {
- if (res.status < 200 || res.status >= 300) {
- throw new Error('Bad response status: ' + res.status);
- }
- let data = res.json();
- console.log(data);
- this.token = data.token;
- this.expiry = data.expiry;
- this.client = data.client;
- localStorage.setItem('access-token', this.token);
- localStorage.setItem('client', this.client);
- localStorage.setItem('expiry', this.expiry);
- localStorage.setItem('uid', this.uid);
- }
- private handleError (error: any) {
- let errMsg = error.json().error || 'Server error';
- console.error(errMsg); // log to console instead
- return Observable.throw(errMsg);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement