4

I am pretty new to Axios and very new to OAuth and Firebase, so I'm sure I'm missing something dumb...

I am trying to create a sign in using firebase's auth provider functions & then create a user profile in my database using Axios. (I have to make a ton of other API calls based on the data I receive and it would be very convenient to just use Axios for everything.)

Here is what I have so far.

  authenticate() {
    var provider = new firebase.auth.GithubAuthProvider();
    console.log(provider);
    firebase.auth().signInWithPopup(provider)
      .then((res) => {
        console.log(res);
        if (res.credential) {
          var token = res.credential.accessToken;
        }
        const user = axios.create({
          baseURL: fbaseUrl,
          withCredentials: true, // newly added
          headers: {
            Authorization: `Bearer ${token}`, // cf firebase docs https://firebase.google.com/docs/reference/rest/database/user-auth
          }
        });
        this.setState({uid: res.user.uid, useraxios: user, token: token});
      }).catch((err) => {
        console.log(err.message);
      }); 
  }

  testPost() {
    this.state.useraxios.post(`/users.json`, { id: this.state.uid, joinedOn: moment() })
      .then((res) => console.log(res))
      .catch((err) => console.log(err.message));  /// this errors out
  }

The error I'm currently getting is that there is no 'Access-Control-Allow-Credentials' header and therefore localhost is not allowed access, which I assume is something in the Firebase rules that I have to sort through. Before I added the withCredentials: true line, I was just getting the "not allowed access" response.

I have also tried

    const user = axios.create({
      baseURL: `${fbaseUrl}/users/${res.user.uid}.json?auth=${token}`
    });

and

      firebase.auth().currentUser.getToken(true).then((token) => {
       const user = axios.create({
          baseURL: `${fbaseUrl}/users/${res.user.uid}.json?auth=${token}`
        });

and

      firebase.auth().currentUser.getToken(true).then((token) => {
       const user = axios.create({
          baseURL: `${fbaseUrl}`,
          headers: {Authorization: token}
        });

as per this stackoverflow question which returns the 401 Unauthorized error.

Posting to the database is totally fine when I have both read & write set to true, so it's not a problem with how I'm formatting the URL or something.

I am assuming there are a couple of problems, one with my axios.create config and another with my Firebase rules, but I have gone through the documentation for both and am still very much at a loss. This is a react app but I'm 98% sure the react stuff isn't the problem.

Am I at least on the right track? (Am I a fool to try to use axios for something that would be better suited to firebase's built-in methods...?) Any help would be deeply appreciated.

Community
  • 1
  • 1
JSilv
  • 1,035
  • 12
  • 26

1 Answers1

1

It is related to your functions configuration. You need to add this in your firebase functions / index.js and configure your function with cors.

const cors = require('cors')({origin: true});

For more details please refer to this url: Enabling CORS in Cloud Functions for Firebase

Er.Se
  • 461
  • 3
  • 11