How to check if user is authenticated in Firebase and Express/Node.js?

前端 未结 1 697
故里飘歌
故里飘歌 2021-01-19 21:55

If I have a page that can only be accessed by authenticated users, how do I check if a user is authenticated or not?

I tried using (firebase.auth().currentUser

1条回答
  •  醉梦人生
    2021-01-19 22:19

    Your code is in an Express app, which means it runs on the server. The Firebase SDK you're using is meant for use on client devices, and won't work well in your Express environment. There is no concept of a "current user" on the server. Of course a user ID can be passed to the server with each request, but the server itself is stateless.

    In your Express server you'll want to use the Firebase Admin SDK. Have a look at this Cloud Functions sample on how to build an authenticated endpoint.

    0 讨论(0)
提交回复
热议问题