Added logout and localStorage

This commit is contained in:
piratefinn 2017-06-05 18:47:34 +01:00
parent d8ff4064e2
commit 1bd586b3ae
5 changed files with 59 additions and 13 deletions

View file

@ -3,16 +3,36 @@ import { Http } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
/* this provider handles the interaction between server and client */
@Injectable()
export class ApiService {
private apiUrl = 'https://dev.app.peartrade.org/api';
private sessionKey: string;
private sessionKey: string = null;
constructor(
private http: Http,
) {}
) {
if (localStorage.getItem('sessionKey') ) {
this.sessionKey = localStorage.getItem('sessionKey');
}
}
private getSessionKey() {
console.log('get key');
return this.sessionKey;
}
private setSessionKey(key) {
console.log('set key');
this.sessionKey = key;
localStorage.setItem('sessionKey', this.sessionKey);
}
private removeSessionKey() {
console.log('remove key');
this.sessionKey = null;
localStorage.removeItem('sessionKey');
}
public getAgeRanges() {
return this.http.get(
@ -33,10 +53,20 @@ export class ApiService {
data
).map( response => response.json() );
login_event.subscribe(
result => { this.sessionKey = result.session_key }
result => { this.setSessionKey(result.session_key) }
);
return login_event;
}
public logout() {
console.log(this.sessionKey);
return this.http.post(
this.apiUrl + '/logout',
{
session_key : this.sessionKey,
}
).map( response => { this.removeSessionKey(); return response.json() } );
}
public search(data) {
data.session_key = this.sessionKey;