1- import { Injectable } from '@angular/core' ;
1+ import { Injectable , Inject } from '@angular/core' ;
22import { Control } from '@angular/common' ;
33import { Http , Response , Headers , RequestOptions , RequestOptionsArgs } from '@angular/http' ;
44import { Observable } from 'rxjs/Observable' ;
@@ -11,66 +11,65 @@ import { User } from './user';
1111
1212@Injectable ( )
1313export class UserService {
14- constructor ( private http : Http ) {
15-
14+ constructor ( private http : Http , @ Inject ( 'apiBase' ) private _apiBase : string ) {
15+
1616 }
1717
18- private _apiBase = 'https://angular2-login-seed.herokuapp.com' ;
1918 private _loginApi = this . _apiBase + '/authorize/local' ;
2019 private _logoutApi = this . _apiBase + '/logout' ;
2120 private _authenticatedApi = this . _apiBase + '/api/authenticated' ;
2221 private _registerApi = this . _apiBase + '/api/users/register' ;
2322 private _userExistsApi = this . _apiBase + '/api/users/exists' ;
24-
23+
2524 login ( user ) {
2625 let body = JSON . stringify ( user ) ;
2726 let headers = new Headers ( ) ;
2827 headers . append ( 'Content-Type' , 'application/json' ) ;
29-
28+
3029 return this . http . post ( this . _loginApi , body , < RequestOptionsArgs > { headers : headers , withCredentials : true } )
3130 . map ( ( res : Response ) => res )
3231 . catch ( this . handleError ) ;
3332 }
34-
33+
3534 authenticated ( ) {
3635 return this . http . get ( this . _authenticatedApi , < RequestOptionsArgs > { withCredentials : true } )
3736 . map ( ( res : Response ) => res . json ( ) )
3837 . catch ( this . handleError ) ;
3938 }
40-
39+
4140 logout ( ) {
4241 return this . http . get ( this . _logoutApi , < RequestOptionsArgs > { withCredentials : true } )
4342 . map ( ( res : Response ) => res . json ( ) )
4443 . catch ( this . handleError ) ;
4544 }
46-
45+
4746 register ( user ) {
4847 let body = JSON . stringify ( user ) ;
4948 let headers = new Headers ( ) ;
5049 headers . append ( 'Content-Type' , 'application/json' ) ;
51-
50+
5251 return this . http . post ( this . _registerApi , body , < RequestOptionsArgs > { headers : headers , withCredentials : true } )
5352 . map ( ( res : Response ) => res )
5453 . catch ( this . handleError ) ;
5554 }
56-
55+
5756 getUsers ( ) {
5857 return this . http . get ( this . _apiBase + "/api/users?limit=5&desc=true" , < RequestOptionsArgs > { withCredentials : true } )
5958 . toPromise ( )
6059 . then ( res => < User [ ] > res . json ( ) , this . handleError )
6160 . then ( data => { console . log ( data ) ; return data ; } ) ; // eyeball results in the console
6261 }
63-
62+
6463 getMe ( ) {
6564 return this . http . get ( this . _apiBase + '/api/users/me/' , < RequestOptionsArgs > { withCredentials : true } )
6665 . toPromise ( )
6766 . then ( res => < User > res . json ( ) . me , this . handleError )
6867 . then ( data => { console . log ( data ) ; return data ; } ) ; // eyeball results in the console
6968 }
70-
69+
7170 private handleError ( error : Response ) {
7271 // in a real world app, we may send the server to some remote logging infrastructure
7372 // instead of just logging it to the console
7473 return Observable . throw ( error || "Server Error" ) ;
7574 }
76- }
75+ }
0 commit comments