From ff79babc6692dc30a70ba377d081d1e75cb86a63 Mon Sep 17 00:00:00 2001
From: Ratnadeep Rajendra Kharade <92khra1mst@hft-stuttgart.de>
Date: Wed, 27 Nov 2019 22:14:11 +0100
Subject: [PATCH] changed token storage mechanism from service based to ionic
 native storage based.

---
 src/app/auth/login/login.page.ts |  4 ++--
 src/app/home/home.page.ts        | 29 ++++++++++++++++-------------
 src/app/rest.service.ts          | 13 ++++++++-----
 3 files changed, 26 insertions(+), 20 deletions(-)

diff --git a/src/app/auth/login/login.page.ts b/src/app/auth/login/login.page.ts
index 130a489..f40332a 100644
--- a/src/app/auth/login/login.page.ts
+++ b/src/app/auth/login/login.page.ts
@@ -12,8 +12,8 @@ import { RestService } from '../../rest.service';
   styleUrls: ['./login.page.scss'],
 })
 export class LoginPage implements OnInit {
-  username = "";
-  password = "";
+  username = "Bob@mail.com";
+  password = "BobPassword";
   correctCredentials = false;
   loginApi: Observable<any>;
 
diff --git a/src/app/home/home.page.ts b/src/app/home/home.page.ts
index d9111a7..e577c54 100644
--- a/src/app/home/home.page.ts
+++ b/src/app/home/home.page.ts
@@ -3,14 +3,17 @@ import { Geolocation } from '@ionic-native/geolocation/ngx';
 import { RestService } from '../rest.service';
 import { Observable } from 'rxjs';
 import { HttpClient, HttpHeaders } from '@angular/common/http';
+import { Storage } from '@ionic/storage';
 
 
 declare var H: any;
+
 @Component({
   selector: 'app-home',
   templateUrl: 'home.page.html',
   styleUrls: ['home.page.scss'],
 })
+
 export class HomePage {
   private platform: any;
   private map: any;
@@ -39,7 +42,8 @@ export class HomePage {
 
   constructor(private geolocation: Geolocation,
     public restService: RestService,
-    public httpClient: HttpClient) {
+    public httpClient: HttpClient,
+    private storage: Storage) {
 
     this.platform = new H.service.Platform({
       'apikey': 'tiVTgBnPbgV1spie5U2MSy-obhD9r2sGiOCbBzFY2_k'
@@ -59,22 +63,21 @@ export class HomePage {
   }
 
   getBikesList() {
-    this.geolocation.getCurrentPosition(
-      {
-        maximumAge: 1000, timeout: 5000,
-        enableHighAccuracy: true
-      }
-    ).then((resp) => {
+    this.geolocation.getCurrentPosition({
+      maximumAge: 1000, timeout: 5000,
+      enableHighAccuracy: true
+    }).then((resp) => {
       this.currentLocation.lat = resp.coords.latitude;
       this.currentLocation.lng = resp.coords.longitude;
-      let url = 'http://193.196.52.237:8081/admin/bikes' + '?lat=' + this.currentLocation.lat + '&lng=' + this.currentLocation.lng;
-      const  headers = new  HttpHeaders().set("Authorization", "Bearer " + this.restService.getToken());
-      this.bikeApi = this.httpClient.get(url, {headers});
-      this.bikeApi
-        .subscribe((resp) => {
+      this.storage.get('token').then((token) => {
+        let url = 'http://193.196.52.237:8081/bikes' + '?lat=' + this.currentLocation.lat + '&lng=' + this.currentLocation.lng;
+        const headers = new HttpHeaders().set("Authorization", "Bearer " + token);
+        this.bikeApi = this.httpClient.get(url, { headers });
+        this.bikeApi.subscribe((resp) => {
           console.log('my data: ', resp);
-          this.bikes = resp.data;
+          this.bikes = resp;
         }, (error) => console.log(error));
+      });
     }, er => {
       alert('Can not retrieve Location')
     }).catch((error) => {
diff --git a/src/app/rest.service.ts b/src/app/rest.service.ts
index 1110494..d015623 100644
--- a/src/app/rest.service.ts
+++ b/src/app/rest.service.ts
@@ -1,19 +1,22 @@
 import { Injectable } from '@angular/core';
+import { Storage } from '@ionic/storage';
 
 @Injectable({
   providedIn: 'root'
 })
 export class RestService {
 
-  token: String;
-  
-  constructor() { }
+  constructor(private storage: Storage) { }
 
   setToken(token) {
-    this.token = token;
+    // set a key/value
+    this.storage.set('token', token);
   }
 
   getToken() {
-    return this.token;
+    this.storage.get('token').then((val) => {
+      console.log('token', val);
+      return val;
+    });
   }
 }
-- 
GitLab