Home > Blockchain >  onAuthStateChanged does not recognize multiple arguments
onAuthStateChanged does not recognize multiple arguments

Time:08-17

// Check if user is logged in
  onAuthStateChanged(async (user: any, _next: any) => {
    if (user) {
      console.log(user);
    } else {
      const result = await signInWithPopup(auth, provider);
      console.log(result);
    }
  });

This code gives the error An argument for 'nextOrObserver' was not provided. Expected 2-4 arguments, but got 1.. But from what I can tell the function has two arguments.

This causes the typescipt compiler to refuse to compile.

CodePudding user response:

But from what I can tell the function has two arguments.

It's pointing out that onAuthStateChange needs 2 arguments, not that the function you write needs 2 arguments. In fact, your function should only have one argument. The piece you're missing is that you need to pass the auth instance into onAuthStateChanged. From the way you're calling signInWithPopup, i assume the variable auth contains that. If not, you can call getAuth to get it:

import { getAuth, onAuthStateChanged, User } from "firebase/auth"
// ...
onAuthStateChanged(
  auth, // or getAuth()
  async (user: User | null) => {
   // ...
  }
);
  • Related