最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - why firebase authentication is slow? - Stack Overflow

programmeradmin0浏览0评论

I am building a login page in my React app using firebase (sign in with google redirect the user method) and it is working but it takes almost two seconds for firebase to get the current user, which is not the best for UX. here is the code

ponentDidMount(){
    console.log(Date.now());
    const auth = getAuth();
    this.authListener = onAuthStateChanged(auth, (user) => {
      if (user) {
        // User is signed in
        console.log(Date.now());
        this.props.dispatch(addUserAction(user.email))
      } 
      else {
        // User is signed out
        this.props.dispatch(addUserAction(null))
      }
    });  
  }
 

what i get in my console is that the difference of time is 1768 milliseconds which is almost 2 seconds, am i doing something wrong ? the console is showing the difference of time as 2 seconds

I am building a login page in my React app using firebase (sign in with google redirect the user method) and it is working but it takes almost two seconds for firebase to get the current user, which is not the best for UX. here is the code

ponentDidMount(){
    console.log(Date.now());
    const auth = getAuth();
    this.authListener = onAuthStateChanged(auth, (user) => {
      if (user) {
        // User is signed in
        console.log(Date.now());
        this.props.dispatch(addUserAction(user.email))
      } 
      else {
        // User is signed out
        this.props.dispatch(addUserAction(null))
      }
    });  
  }
 

what i get in my console is that the difference of time is 1768 milliseconds which is almost 2 seconds, am i doing something wrong ? the console is showing the difference of time as 2 seconds

Share Improve this question edited Sep 28, 2021 at 12:59 Frank van Puffelen 601k85 gold badges890 silver badges860 bronze badges asked Sep 28, 2021 at 7:02 Amr HeshamAmr Hesham 431 silver badge4 bronze badges 3
  • FYI performance.now() is a better fit for metrics – Phil Commented Sep 28, 2021 at 7:09
  • Thanks, i am still sure it is taking way more than it should – Amr Hesham Commented Sep 28, 2021 at 7:29
  • Because it IS slow and for this reason you should avoid using it. Build your own authentication, like I did and you'll discover how significantly faster it is. – Johann Commented Jan 14, 2022 at 7:35
Add a ment  | 

1 Answer 1

Reset to default 5

When you restart the app/reload the page, Firebase automatically restores the user's authentication state based on the information it stored in local storage when the user first signed in. For this it does make a call to the server though, to check whether the credentials are still valid - and for example to ensure the account hasn't been disabled. It's likely that this call is what is taking time in your use-case.

A mon trick is to make your own determination on whether the server-check is likely to succeed based on only client-side information. For this, store an extra value in local storage when the user signs in successfully, say isAuthenticated. Now when you reload the page/app, you an read this value from local storage, and then the user was previously authenticated, assume that they will be authenticated again.

The assumption may be wrong of course, so you'll have to handle that scenario too in your code.

Also see this talk Architecting Mobile Web Apps, where Michael Bleigh talks about the technique.

发布评论

评论列表(0)

  1. 暂无评论