How to reinject data in a global provider

hi everybody,
my problem : i have a global provider where I want to inject all variables needed for my app.
and i have TWO others providers (network and webwservices )that need to interact with this global provider.
My NetWork Provider check some basics fonctionnality and get the uuid device, No problem to inject this data in my globalProvider. I’m using this data again in my Webservices provider wich is use to interact with my Api (get and update data). So I post this data to my Api and get a object response with a token.
I’d like to reinject this token in my global Provider but it doesn’t work.I walways get my dat undefine in my global Provider.
here is my code :
webservicesProvider.ts

return this.global.apiToken;this.http.post(this.url_api + '?action=getToken', dataToPost)
                .subscribe(data => {
                    console.log(data);
                    
                     console.log(data['status']); 
                    if (data['status']==1){
                        console.log(data['token']);
                        this.global.apiToken = data['token'];
                        
                        console.log(this.global.apiToken);
                    }else{
                        console.log('erreur status');
                    }    

and in my globalProvider.ts :

 /***********Token *********/
    public apiToken : string;
    public decryptToken : string;

i just made a console.log of my apiToken in the constructor and it returned Undefined.

Does somebody can help me ?

The bit you posted from webservicesProvider.ts confuses me. How is it ever going to get to the http.post call if that method has already returned?

sorry, Itried a lot of solutions and my brain get damage at this moment !!! I’m new with ionic and angular so I think there is some way to work with that I don’t rtellay understand at this moment.

So i give here more details :

Web-services.ts :

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';

import 'rxjs/add/operator/map';


import { GlobalProvider } from './global';



/*
  Generated class for the WebServicesProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()

export class WebServicesProvider {
    

    url_api: string = this.global.urlWeb + 'api';
    uuid = this.global.uuid;
    privateKey = this.global.privateHashKey;
     
 
    constructor(private http: HttpClient,
        public global?: GlobalProvider) {

        console.log('Hello WebServicesProvider Provider');
        console.log('uuid=' + this.uuid + 'privatekeys is' + this.privateKey);

        
    }

  public getToken() {
        let dataToPost = new FormData();
        dataToPost.append('uuid',this.uuid)
        dataToPost.append('private_key',this.privateKey);
        console.log(dataToPost);
    //    /************** fonction OK ********** */
        this.http.post(this.url_api + '?action=getToken', dataToPost)
                .subscribe(data => {
                    console.log(data);
                    
                     console.log(data['status']); 
                    if (data['status']==1){
                        console.log(data['token']);
                        this.global.apiToken = data['token'];
                       console.log( this.global.apiToken);
                         
                    }else{
                        console.log('erreur status');
                    }    
                });
    };

}

global.ts :

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { InAppBrowser } from '@ionic-native/in-app-browser';
import { Platform, Events, NavController, AlertController } from 'ionic-angular';
import { Network } from '@ionic-native/network';

import CryptoJS from 'crypto-js';
import 'rxjs/add/operator/map';

import { NetworkProvider } from '../providers/network';
import { WebServicesProvider } from '../providers/web-services';





/*
  Generated class for the GlobalProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class GlobalProvider {


    constructor(public http: HttpClient,
        public plt: Platform,
        public events: Events,
        public network: Network,
        public iab: InAppBrowser,
        public networkProvider: NetworkProvider,


    ) {
        console.log('Hello GlobalProvider Provider');

        this.hashSha1();
    }



    //access to website
    public urlWeb: string = '**********';
    goToWebsite() {
        console.log('click');
        const browser = this.iab.create(this.urlWeb);
        browser.show();
    }

    /*************ACCESS TO API **********************/

    public privateKey: string = '***REDACTED***';
    url_api = this.urlWeb + 'api';
    uuid = this.networkProvider.device.uuid;
    public privateHashKey: any;

    /* crypt and decrypt  HASH  */
    hashSha1() {
        this.privateHashKey = CryptoJS.SHA1(this.privateKey).toString();
        console.log("hashKey =" + this.privateHashKey);
    };
    /***********Token *********/
    public apiToken: string;
    public decryptToken: string;



}

startApp.ts :

import { Component } from '@angular/core';
import { NavController, App } from 'ionic-angular';
import { ToastController } from 'ionic-angular';


import { NetworkProvider } from '../../providers/network';
import { GlobalProvider } from '../../providers/global';
import { WebServicesProvider } from '../../providers/web-services';

import 'rxjs/add/operator/map';

import { HomePage } from '../home/home';
import { NavPage } from '../nav/nav';









/**
 * Generated class for the StartAppPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@Component({
    selector: 'page-start-app',
    templateUrl: 'start-app.html',
})
export class StartAppPage {


    constructor(

        public networkProvider: NetworkProvider,
        public global: GlobalProvider,
        public webServices: WebServicesProvider,

        public nav: NavController,
        public Toast: ToastController,
        public appCtrl: App,
    ) { }
    homePage = HomePage;


    ionViewDidLoad() {
        console.log('ionViewDidLoad StartAppPage');
this.webServices.getToken();
console.log(this.global.apiToken);

    }
    ionViewWillEnter() {
        console.log('ionwiew will enter');


    }



    goToApp() {
        this.nav.push(NavPage);
    }
    goToWebSite() {
        this.global.goToWebsite();
    }





    //************ */ TEST FABRICE***************
    // ionViewDidEnter(){
    //     this.checkNetwork();
    // }
    //  checkNetwork(){
    //       this.goToApp('into network');
    // this.network.onDisconnect().subscribe(() => {
    //     console.log('network was disconnected :-(');
    //     this.goToApp('network was disconnected :-(');
    //   });


    // this.network.onConnect().subscribe(() => {
    //   console.log('network connected!');
    //    this.goToApp('network connected!');
    //   // We just got a connection but we need to wait briefly
    //    // before we determine the connection type. Might need to wait.
    //   // prior to doing any api requests as well.
    //   setTimeout(() => {
    //     if (this.network.type === 'wifi') {
    //       console.log('we got a wifi connection, woohoo!');
    //        this.goToApp('we got a wifi connection, woohoo!');
    //     }
    //   }, 3000);
    // });

    // // stop connect watch

    //  }

    // goToApp(_message:string="message vide") {
    //     // this.navCtrl.push(HomePage);  
    //     let alertTest = this.alertController.create({
    //         title:"test",    
    //         message : _message,

    //         buttons: ['Cancel']
    //         });
    //         alertTest.present();

    // } 
}



and here what i get in my console :
network.ts:32 Hello NetworkProvider Provider
go check
go check
core.js:3688 Angular is running in the development mode. Call enableProdMode() to enable the production mode.
util.js:44 Ionic Native: tried calling Device.uuid, but the Device plugin is not installed.
pluginWarn @ util.js:44
util.js:56 Install the Device plugin: ‘ionic cordova plugin add cordova-plugin-device’
pluginWarn @ util.js:56
global.ts:36 Hello GlobalProvider Provider
global.ts:60 hashKey =cc2298fea7bba0fa3da011488fd6e7403520dd0e
web-services.ts:30 Hello WebServicesProvider Provider
web-services.ts:31 uuid=nullprivatekeys iscc2298fea7bba0fa3da011488fd6e7403520dd0e
start-app.ts:51 ionViewDidLoad StartAppPage
web-services.ts:40 FormData
start-app.ts:53 undefined
start-app.ts:57 ionwiew will enter
cordova.js:1021 adding proxy for Device
cordova.js:1021 adding proxy for SplashScreen
cordova.js:1021 adding proxy for StatusBar
cordova.js:1021 adding proxy for NetworkStatus
cordova.js:1021 adding proxy for InAppBrowser
cordova.js:1021 adding proxy for File
cordova.js:1021 adding proxy for CordovaHttpPlugin
Preparing.js:174 Persistent fs quota granted
StatusBarProxy.js:23 StatusBar is not supported
screen:1 Failed to load resource: the server responded with a status of 404 (Not Found)
bootstrap.js:10 Ionic Native: deviceready event fired after 891 ms
app.component.ts:36 Platform ready from cordova
app.component.ts:54 in check
web-services.ts:44 Object
web-services.ts:46 1
web-services.ts:48 d3b4639706066a400c3cbb6f4548628eec1c13ae53250e3993b8009dfc7ad968
web-services.ts:50 d3b4639706066a400c3cbb6f4548628eec1c13ae53250e3993b8009dfc7ad968

I think my apiToken get undefined in my Sartapp Page because i call it back before the web service provider finissh.
But I dont know how to change that; if You can explain me that will be nice !!!

There are much larger issues here, especially the homebrew security protocol. If I were you, I would immediately start using JWT instead. Baking secrets into the source code of your app renders them meaningless. Anybody with a copy of the app binary has that private key that I edited out of your post.

Changing all of this to use a JWT library will likely design away the problems you are having here, especially when combined with searching for ways to use them with HttpInterceptors. I think I’ve posted a couple here, and there are doubtless zillions more across the web.