Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolving promise before initializing datastore #98

Closed
jamesdixon opened this issue Sep 4, 2017 · 0 comments
Closed

resolving promise before initializing datastore #98

jamesdixon opened this issue Sep 4, 2017 · 0 comments

Comments

@jamesdixon
Copy link

Hi,

As in #39, I'm attempting to set an authorization header in my data store provider to avoid having to do this for each call.

I'm using Ionic's Storage library to store and retrieve the token I'll be passing along with the header. Unfortunately, the call to retrieve the token returns a Promise and because of this, by the time the provider is initialized and the HTTP request has been made, the header has not yet been set.

Does anyone know a way this can be done?

Thanks!

Relevant code:

import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { JsonApiDatastoreConfig, JsonApiDatastore } from 'angular2-jsonapi';
import { Storage } from '@ionic/storage';
import { Customer } from "../../models/customer";
import { AuthProvider } from "../auth/auth";

@Injectable()
@JsonApiDatastoreConfig({
  baseUrl: 'https://localhost:3001/',
  models: {
    Customer
  }
})
export class DataProvider extends JsonApiDatastore {

  constructor(http: Http, private _auth: AuthProvider, private _storage: Storage) {
    super(http);
    this.setHeaders();
  }

  setHeaders() : void {
    this._auth.getToken().then((token) => {
      this.headers = new Headers({ 'Authorization': token });
    });
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant