You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.1 KiB

5 months ago
  1. import React, { createContext, useContext, useEffect, useState } from "react";
  2. import auth, { FirebaseAuthTypes } from "@react-native-firebase/auth";
  3. import { useRouter } from "expo-router";
  4. interface LoginState {
  5. tempEmail?: string;
  6. passwordReset?: boolean
  7. }
  8. const AuthContext = createContext<
  9. [
  10. FirebaseAuthTypes.User | null,
  11. LoginState | null,
  12. React.Dispatch<React.SetStateAction<LoginState | null>> | null,
  13. boolean
  14. ]
  15. >([null, null, null, false]);
  16. export default function AuthProvider(props: { children: React.ReactNode }) {
  17. const [user, setUser] = useState<FirebaseAuthTypes.User | null>(null);
  18. const [init, setInit] = useState(true);
  19. const [loginstate, setLogInState] = useState<LoginState|null>(null);
  20. useEffect(() => {
  21. const subscriber = auth().onAuthStateChanged((u) => {
  22. setUser(u);
  23. if (init) {
  24. setInit(false);
  25. }
  26. return subscriber;
  27. });
  28. }, []);
  29. if (init) return null;
  30. return (
  31. <AuthContext.Provider value={[user, loginstate, setLogInState, init]}>
  32. {props.children}
  33. </AuthContext.Provider>
  34. );
  35. }
  36. export function useAuthState() {
  37. return useContext(AuthContext);
  38. }