1

Basically my problem is, I have my callable functions on Firebase where I want to use "context" to identify if the user is authenticated or not. In the front-end I am logging in user using Firebase authentication (which is an http function on firebase), and as I result I get my user token (which should be used as a Bearer token in the authorization header). The problem is I am not sure how to set the header when I sign in the user so that my "context.auth" would contain the logged in user info rather than being empty. I use firebase.functions().httpsCallable('myFunction'); as the document suggests to make the call from front-end where the problem is even though I logged in before making this call, my context is null.

To give more context think about the following scenario,

//Backend (deployed to cloud functions)
exports.signout = functions.https.onCall((data, context) => {
    if(context.auth){
        //do signout stuff and return true
    }
    else{
        //not logged in so you can't sign out return false
    }
});


//Client
let signout = firebase.functions().httpsCallable('signout');
signout()
    .then(res => console.log("signed out"))
    .catch(err => console.log(err))

So simply put, while making the httpsCallable('signout') in client, I should have the user token in the 'Authorization' header according to docs, so that I can access the context.auth from my callable function. The thing that I don't understand is how that header should be set there? The most logical thing is setting it on login, but it is not something like setting default header for axios since the call is not exactly an http request rather we use that special httpsCallable function. So how/when is that auth header should be set?

Ali Beyit
  • 431
  • 1
  • 6
  • 19
  • Can you share your code? Cloud Function as well as authentication mechanism. – Renaud Tarnec Aug 01 '20 at 12:45
  • @RenaudTarnec I have just updated my question to explain what I couldn't understand about the callable function mechanism. – Ali Beyit Aug 01 '20 at 13:29
  • You say "In the front-end I am logging in user using Firebase authentication (which is an http function on firebase)". How do you exactly authenticate? Do you use the [JavaScript SDK](https://firebase.google.com/docs/auth/web/start)? If no, what is the reason? – Renaud Tarnec Aug 01 '20 at 13:39
  • I am using `firebase.auth().signInWithEmailAndPassword` in my signin function which is in the cloud. – Ali Beyit Aug 01 '20 at 14:02
  • If you are using `firebase.auth().signInWithEmailAndPassword()` it should work: **after successful authentication, the Firebase Authentication token is automatically included in the requests to the Callable Function**. There is probably an error in your Authentication code. You should share it for us to be able to help you. – Renaud Tarnec Aug 01 '20 at 14:11
  • I am having this same problem with Gen 2 callable functions. – mabarif Jul 12 '23 at 01:42

1 Answers1

4

When you use a callable type function from a web or mobile client using the provided SDK, all of the details of the HTTP protocol are handled automatically. There's nothing you have to do to set any headers.

If the user is currently signed in at the time of the request, the SDK will add the authorization header automatically. If the user is signed out, then no header will be added. So, if you want to invoke signout with the authorization of the end user, you will obviously have to call it while they are signed in.

It sounds like you might have signed out the user before invoking the callable. In that case, your function will receive no user data.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Yeah, I don't think that's the case as my `signout` function was not actually doing anything related to sign out at that point. So I believe the problem is I am not calling `firebase.auth().signInWithEmailAndPassword()` from the client-side rather I am calling an endpoint which does signing in inside that function (function run on the server/cloud). So if I actually run the firebase sign in from frontend then it should work. Correct me if I am wrong. – Ali Beyit Aug 01 '20 at 19:36
  • You can only sign in from the frontend. There is no backend signin. – Doug Stevenson Aug 01 '20 at 19:39
  • @DougStevenson This does not appear to be the answer. I am signing my user in before making the call. To double check, I get the user and print out the info in the console. All is as expected. but when I call the callable function (gen2) The user and auth data is missing. – mabarif Jul 12 '23 at 01:39
  • @mabarif The answer is stating correct information - I know this because I was on staff at Firebase when this feature was released. If your code has a problem, then you should post a new question, and share your [minimal complete reproducible example](https://overflow.tips/write-good-question/minimal-complete-reproducible-example), along with your debugging details. – Doug Stevenson Jul 12 '23 at 01:53
  • @DougStevenson Ah of course, I think my problem was the emulator. You can see here there was a bug reported https://github.com/firebase/firebase-tools/issues/5210 (and eventually fixed). *But*, this problem still exists for 2nd gen functions, which I was using. I had to revert back to 1st gen functions to solve the problem. – mabarif Jul 12 '23 at 02:17