0
votes

First-time Stack Overflow poster here!

I’m following an Angular 4 tutorial, and completing its authentication section with Firebase (link: ). I was able to successfully signup and login, but receive errors when passing the user’ token, via ‘getIdToken’, to my GET request to limit certain actions to authenticated users.

When passing my token to my GET request, I get the following error:

* Response {_body: "{↵  "error" : "Could not parse auth token."↵}↵", status: 401, ok: false, statusText: "Unauthorized", headers: Headers…}

I also experience this issue when copying and pasting tokens from the console into code

I’ve posted the (potentially) most relevant code below for debugging:

header.component.ts

  onFetchData() {
    this.dataStorageService.getRecipes();
  }

data-storage.service.ts

  getRecipes() {

    const token = this.auth.getTokenGrabber() 

    this.http.get('https://recipes-fe1ba.firebaseio.com/.json?auth=' + token)
      .map(
        (response: Response) => {
          console.log(response.json())
          const recipes: Recipe[] = response.json();
          for (let recipe of recipes) {
            if (!recipe['ingredients']) {
              recipe['ingredients'] = [];
            }
          }
          return recipes;
        }
      )
      .subscribe(
        (recipes: Recipe[]) => {
          this.recipeService.setRecipes(recipes);
        }
      );
  }

authentication.service.ts

 signinUser(email, password){

    firebase.auth().signInWithEmailAndPassword(email, password)
      .then(
        (response) => {
              firebase.auth().currentUser.getIdToken(
               ).then(
                  (token:string) => {
                    // console.log(token)  
                    this.token = token 
                    console.log('user was found')
                    // console.log(this.token)
                  }
               )
        }
      ).catch((erorr) => {
        console.log('user was not found')    
      }
    )  
  }

  getTokenGrabber(){
            firebase.auth().currentUser.getIdToken()
              .then(
                  (token:string) =>

                    this.token = token 
               )
               return this.token
        }
}
1
This is a long shot because you have not posted since this time. But did you ever figure out this issue? - Jeremy

1 Answers

0
votes

The REST API documentation indicates the query parameter is access_token, not auth. Give this a try:

this.http.get('https://recipes-fe1ba.firebaseio.com/.json?access_token=' + token)