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

I have an React app with authentication and a firebase backend.How do i check if the user is signed in

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;
}
  

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745110583a4611826.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信