简体   繁体   中英

Firebase Node.js- onAuthStateChanged() : How to redirect to different page?

As the onAuthStateChanged() function in Firebase does not accept any res/req parameters, nor does window.location work on Node.js (because it is server-side code), how do I redirect to another page after auth state changes?

For reference what I had was:

firebase.auth().onAuthStateChanged(firebaseUser=>{
    if(user){
         window.location = "/profile";   
    }
});

I believe you're using the client side Firebase SDK. If you're developing inside of Node you'll have to use the Admin SDK. I believe it gives you more control over authentication.

https://firebase.google.com/docs/auth/admin/

What you're trying to do would work well on the client but doing it on the server would require a completely different approach. One might be using a Cloud Function to have the user logout. You could handle the session manually and redirect as needed.

However, The code you're writing now is better suited to doing this over on the client.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM