16.5 C
London
Wednesday, September 4, 2024

My createContext() wont work with @react-native-firebase/auth


I created a useContext to entry my react-native-firebase/auth gloabally.

Im runnning the app utilizing expo run:ios I’ve achieved pod replace and no errors.

It is working completely when Creating an account however not when i am making an attempt to Signal In it’s going to present that consumer=null.

My file tree for my AuthProvider is:

AuthProvider File Tree

and My App Screens are like this:

App Screens

I am utilizing expo-router and that is my package deal.json to my foremost part

"foremost": "expo-router/entry",

For Some motive my AuthProvider this error:

LOG: Consumer NOT SIGNED IN

I’ve applied this perform in my dashboard.js: ( i’ve imported useAuth() )

  const { consumer } = useAuth();
  const router = useRouter();

  useEffect(() => {
    if (!consumer) {
      console.log("Consumer NOT SIGNED IN");
      router.push("/(house)/login"); // Redirect to login if no consumer
    } else {
      console.log("Consumer SIGNED IN: ", consumer.electronic mail);
    }
  }, [user, router]);

That is my Redirect.js:

import React from "react";
import { Redirect } from "expo-router";
import useAuth from "../src/hooks/useAuth";

const Index = () => {
  const { consumer } = useAuth();
  console.log("Consumer:", consumer);

  if (!consumer) {
    // If there is no consumer, redirect to login
    return <Redirect href="/(house)/login" />;
  }

  // If there's a consumer, redirect to dashboard
  return <Redirect href="/(tabs)/dashboard" />;
};

export default Index;

My index.js:

import React from "react";
import { AuthProvider } from "../src/context/AuthProvider";
import RedirectComponent from "../parts/RedirectComponent";

const Index = () => {
  return (
    <AuthProvider>
      <RedirectComponent />
    </AuthProvider>
  );
};

export default Index;

That is my SignIn perform in my AuthServices.js:

 signIn: async (electronic mail, password) => {
    attempt {
      const response = await auth().signInWithEmailAndPassword(electronic mail, password);
      Alert.alert(electronic mail, "Has efficiently logged in!");
      return response.consumer; // Ensure that this returns the consumer object
    } catch (error) {
      Alert.alert("Login Failed", error.message);
      throw error;
    }
  },

That is my _layout.js:

// ./app/(tabs)/_layout.js
import React from "react";
import AuthProvider from "../../src/context/AuthProvider";
import AppRoutes from "./(router)";

const Structure = () => {
  return (
    <AuthProvider>
      <AppRoutes />
    </AuthProvider>
  );
};
export default Structure;
Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here