Config and login
Change-Id: I81ffb9b8097620cb7870beaf1b1b315945eebec0
diff --git a/src/app/services/rest/auth.service.ts b/src/app/services/rest/auth.service.ts
new file mode 100644
index 0000000..7c73c28
--- /dev/null
+++ b/src/app/services/rest/auth.service.ts
@@ -0,0 +1,48 @@
+/// <reference path="../../../../typings/index.d.ts"/>
+
+// Imports
+import {AppConfig} from '../../config/app.config';
+import {Injectable} from '@angular/core';
+import {Http, Response} from '@angular/http';
+import {Observable} from 'rxjs/Rx';
+import {IAuthRequest, IAuthResponse} from '../../interfaces/auth.interface';
+import {CookieService} from 'angular2-cookie/core';
+
+// Import RxJs required methods
+import 'rxjs/add/operator/map';
+import 'rxjs/add/operator/catch';
+
+@Injectable()
+export class AuthService {
+ private xosToken:string;
+ private xosSessionId:string;
+ // Resolve HTTP using the constructor
+ constructor (private http: Http, private cookieService:CookieService) {
+ }
+
+ // check if the user is authenticated
+ isAuthenticated(){
+ this.xosToken = this.cookieService.get('xoscsrftoken');
+ this.xosSessionId = this.cookieService.get('xossessionid');
+ return this.xosToken;
+ }
+
+ // save cookies
+ storeAuth(auth:IAuthResponse){
+ this.cookieService.put('xoscsrftoken', auth.xoscsrftoken);
+ this.cookieService.put('xossessionid', auth.xossessionid);
+ }
+
+ // Log the user in
+ login(auth: IAuthRequest) : Observable<IAuthResponse> {
+ return this.http.post(`${AppConfig.apiEndpoint}/utility/login/`, auth)
+ .map((res:Response) => res.json())
+ .map((auth:IAuthResponse) => {
+ this.storeAuth(auth);
+ auth.user = JSON.parse(auth.user);
+ return auth;
+ })
+ .catch((error:any) => Observable.throw(error.json().error || 'Server error'));
+ }
+}
+
diff --git a/src/app/services/rest/core.service.ts b/src/app/services/rest/core.service.ts
new file mode 100644
index 0000000..04097a6
--- /dev/null
+++ b/src/app/services/rest/core.service.ts
@@ -0,0 +1,31 @@
+/// <reference path="../../../../typings/index.d.ts"/>
+
+// Imports
+import {AppConfig} from '../../config/app.config';
+import { Injectable } from '@angular/core';
+import { Http, Response, Headers, RequestOptions } from '@angular/http';
+import {Observable} from 'rxjs/Rx';
+
+// Import RxJs required methods
+import 'rxjs/add/operator/map';
+import 'rxjs/add/operator/catch';
+
+@Injectable()
+export class CoreService {
+ // Resolve HTTP using the constructor
+ constructor (private http: Http) {}
+ // private instance variable to hold base url
+ private baseUrl = AppConfig.apiEndpoint;
+
+ // Fetch all existing comments
+ getCoreEndpoints() : Observable<any[]> {
+
+ // ...using get request
+ return this.http.get(`${this.baseUrl}/core/`)
+ // ...and calling .json() on the response to return data
+ .map((res:Response) => res.json())
+ //...errors if any
+ .catch((error:any) => Observable.throw(error.json().error || 'Server error'));
+
+ }
+}