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


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

Im runnning the app using expo run:ios I’ve done pod update and no errors.

Non of the components are working properly anymore it was working fine when register but i don’t know whats happened.

My file tree for my AuthProvider is:

AuthProvider File Tree

and My App Screens are like this:

App Screens

I’m using expo-router and this is my package.json to my main component

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

For Some reason my AuthProvider this error:

LOG: User NOT SIGNED IN

I have implemented this function in my dashboard.js: ( i have imported useAuth() )

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

  useEffect(() => {
    if (!user) {
      console.log("User NOT SIGNED IN");
      router.push("/(home)/login"); // Redirect to login if no user
    } else {
      console.log("User SIGNED IN: ", user.email);
    }
  }, [user, router]);

This is my Redirect.js:

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

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

  if (!user) {
    // If there's no user, redirect to login
    return <Redirect href="/(home)/login" />;
  }

  // If there is a user, 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 "../components/RedirectComponent";

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

export default Index;

This is my SignIn function in my AuthServices.js:

 signIn: async (email, password) => {
    try {
      const response = await auth().signInWithEmailAndPassword(email, password);
      Alert.alert(email, "Has successfully logged in!");
      return response.user; // Make sure this returns the user object
    } catch (error) {
      Alert.alert("Login Failed", error.message);
      throw error;
    }
  },

This is my _layout.js:

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

const Layout = () => {
  return (
    <AuthProvider>
      <AppRoutes />
    </AuthProvider>
  );
};
export default Layout;

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img