Mount Ain
Mount Ain

Reputation: 406

How to find user is logged in or not from cloud function side using Firebase?

To provide dynamic content delivery, I am using rewrites in fire base hosting. Whenever open website with index.html then the browser request the firebase cloud function main.

"rewrites": [ {
      "source": "/index.html",
      "function":"main"
}]

Now I am facing a problem to provide dynamic content based on user login status. I also checked about client side authendication using JS.

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
  } else {
    // No user is signed in.
  }
});

I don't know about web development. Here I have few questions,

How can I find authentication status of user by more flexible way? Does cookies used for this? I am asking because I don't know to pass firebase-token to the cloud function main.

Please address me an idea. Thank you all.

Upvotes: 0

Views: 106

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317372

Short answer: End users don't have a sign-in status that's visible on a backend. That's just not how Firebase Authentication works.

Auth clients provide credentials to get a token that's used to identify themself when they invoke backend services. This tokens has a lifetime of 1 hour, and the client must refresh it in order to keep using it. Your backend doesn't know or care if the client has an auth token, if they use it, or if they refresh it. The client just needs to provide that token from whatever device they have signed in so the backend can validate it. There is no way your backend can know if the client obtained a token - you just have to accept the one it is given. This means you're going to have to actually figure out how to pass that token and validate it with the Firebase Admin SDK, or use a callable type function using the Firebase Client SDK to send that token automatically.

Upvotes: 2

Related Questions