javascript - I want to check if a user is signed in with firebase - Stack Overflow

admin2025-04-20  0

I have an React app with authentication and a firebase backend. How do i check if the user is signed in? I tried it with onAuthStateChanged. But it only returns:

Here is my code:

import * as FirestoreService from '../../firebase/firebase';

 useEffect(() => {
    let user = FirestoreService.isLoggedIn()
        
        console.log( user)
    
}, []);

Now in the firebase file:

 export const isLoggedIn = () => {
    return firebase.auth().onAuthStateChanged(function (user) {
    if (user) {
      console.log("User signed in");
      
    }
    else {
      console.log("User signed out");
      
    }
  });
  
}

My Check if Loggin was successfull:

function handleLogin() {
     FirestoreService.LoggIn(email, password).then(function (result) {
        // result.user.tenantId should be ‘TENANT_PROJECT_ID’.
        setloggedIn(true)
        
    }).catch(function (error) {
        
        setloggedIn(false);
    });
}

Firebase File:

    export const LoggIn = (email, password) => {
  return firebase.auth().signInWithEmailAndPassword(email, password)
    .then(function (result) {
      // result.user.tenantId should be ‘TENANT_PROJECT_ID’.
      return result;
    })
    .catch(function (error) {
      return error
    });
}

I have an React app with authentication and a firebase backend. How do i check if the user is signed in? I tried it with onAuthStateChanged. But it only returns:

Here is my code:

import * as FirestoreService from '../../firebase/firebase';

 useEffect(() => {
    let user = FirestoreService.isLoggedIn()
        
        console.log( user)
    
}, []);

Now in the firebase file:

 export const isLoggedIn = () => {
    return firebase.auth().onAuthStateChanged(function (user) {
    if (user) {
      console.log("User signed in");
      
    }
    else {
      console.log("User signed out");
      
    }
  });
  
}

My Check if Loggin was successfull:

function handleLogin() {
     FirestoreService.LoggIn(email, password).then(function (result) {
        // result.user.tenantId should be ‘TENANT_PROJECT_ID’.
        setloggedIn(true)
        
    }).catch(function (error) {
        
        setloggedIn(false);
    });
}

Firebase File:

    export const LoggIn = (email, password) => {
  return firebase.auth().signInWithEmailAndPassword(email, password)
    .then(function (result) {
      // result.user.tenantId should be ‘TENANT_PROJECT_ID’.
      return result;
    })
    .catch(function (error) {
      return error
    });
}
Share Improve this question edited Jun 27, 2020 at 12:21 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Jun 27, 2020 at 9:30 user11425419user11425419
Add a ment  | 

1 Answer 1

Reset to default 7

onAuthStateChanged() adds an observer for changes to the user's sign-in state but does not return a user object.

In your case you should use the currentUser property. If a user isn't signed in, currentUser is null:

export const isLoggedIn = () => {
    return firebase.auth().currentUser;
}
  
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745110584a285560.html

最新回复(0)