0

I have got a Login page where if the user has already registered then the login page for CometChat should appear and after logging in the user will redirected to home page. The authentication part is handled using Firebase. But on clicking the Login button with correct credentials , the user is not getting redirected to the Comet Chat login page. Here is the code snippet:

if (isUserCredentialsValid(email, password)) {
      // if the user's credentials are valid, call Firebase authentication service.
      signInWithEmailAndPassword(auth, email, password).then((userCredential) => {
        const userEmail = userCredential.user.email;
        onValue(
          query(ref(realTimeDb, 'users'), orderByChild('email'), equalTo(userEmail)),
          function (snapshot) {
            const val = snapshot.val();
            if (val) {
              const keys = Object.keys(val);
              const user = val[keys[0]];
              // login cometchat.
              cometChat
                .login(user.id, `${process.env.REACT_APP_COMETCHAT_AUTH_KEY}`)
                .then(
                  (User) => {
                    // User logged in successfully.
                    // save authenticated user to local storage.
                    localStorage.setItem('auth', JSON.stringify(user));
                    // save authenticated user to context.
                    setUser(user);
                    // hide loading.
                    setIsLoading(false);
                    // redirect to home page.
                    history('/');
                  },
                  (error) => {
                    // User login failed, check error and take appropriate action.
                  }
                );
            }
          }
        );
      }).catch((error) => {
        // hide loading indicator.
        setIsLoading(false);
        alert(`Your user's name or password is not correct`);
      });
    } else {
      // hide loading indicator.
      setIsLoading(false);
      alert(`Your user's name or password is not correct`);
    }

I tried to check whether there is any syntax errors or not but could not find any.Even the console does not report any error.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

0 Answers0