Skip to content

Commit 9cdbf18

Browse files
committed
Set API end point as a provide for services
1 parent 639ba46 commit 9cdbf18

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Injectable } from '@angular/core';
1+
import { Injectable, Inject } from '@angular/core';
22
import { Control } from '@angular/common';
33
import { Http, Response, Headers, RequestOptions, RequestOptionsArgs } from '@angular/http';
44
import { Observable } from 'rxjs/Observable';
@@ -11,66 +11,65 @@ import { User } from './user';
1111

1212
@Injectable()
1313
export 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+
}

src/main.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { bootstrap } from '@angular/platform-browser-dynamic';
2-
import { enableProdMode } from '@angular/core';
2+
import { enableProdMode, provide } from '@angular/core';
33
import { HTTP_PROVIDERS } from '@angular/http';
44
import { disableDeprecatedForms, provideForms } from '@angular/forms';
55
import { Angular2LoginSeedAppComponent, environment } from './app';
@@ -10,5 +10,11 @@ if (environment.production) {
1010
enableProdMode();
1111
}
1212

13-
bootstrap(Angular2LoginSeedAppComponent, [APP_ROUTER_PROVIDERS, HTTP_PROVIDERS, UserService, disableDeprecatedForms(), provideForms()]);
14-
13+
bootstrap(Angular2LoginSeedAppComponent, [
14+
APP_ROUTER_PROVIDERS,
15+
HTTP_PROVIDERS,
16+
provide('apiBase', {useValue: 'https://angular2-login-seed.herokuapp.com'}),
17+
UserService,
18+
disableDeprecatedForms(),
19+
provideForms()
20+
]);

0 commit comments

Comments
 (0)