here you go loewy

This commit is contained in:
Andy Malkin
2024-02-07 16:59:11 -08:00
parent b9221fa949
commit 3a476e0db4
5 changed files with 69 additions and 54 deletions

View File

@@ -1,8 +1,7 @@
import { DEV_USER_ID } from "@env";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { FirebaseAuthTypes } from "@react-native-firebase/auth";
import React, { createContext, useContext, useEffect, useState } from "react";
import { handleSignOut, onAuthStateChanged } from "../auth/firebase-auth";
import { currentUser, handleSignOut, onAuthStateChanged } from "../auth";
import { useAuthHeader } from "../graphql/client";
interface AuthContextType {
@@ -19,25 +18,44 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({
}) => {
const { setAuthHeader } = useAuthHeader();
const [user, setUser] = useState<FirebaseAuthTypes.User | null>(null);
const [contextUser, setContextUser] = useState<FirebaseAuthTypes.User | null>(
null,
);
const [isLoggedIn, setIsLoggedIn] = useState<boolean>(false);
const [isLoading, setIsLoading] = useState<boolean>(true); // this is for a LoadingContext (auth, app reloads, foreground etc)
const authStateChangeCallback = async (user) => {
console.log("user:", user);
const _completeAuthentication = (
user: FirebaseAuthTypes.User,
token: string,
isLoggedIn: boolean,
) => {
setAuthHeader({ key: "Authorization", value: token });
setContextUser(user);
setIsLoggedIn(isLoggedIn);
setIsLoading(false);
};
const authStateChangeCallback = async (user: FirebaseAuthTypes.User) => {
if (user) {
const token = await user.getIdToken();
if (token) {
await AsyncStorage.setItem("token", token);
setAuthHeader({ key: "Authorization", value: token });
setUser(user);
setIsLoggedIn(true);
setIsLoading(false)
}
_completeAuthentication(user, token, true);
} else {
setIsLoggedIn(false);
_completeAuthentication(undefined, undefined, false);
}
}
};
useEffect(() => {
let unsubscribe = () => {
console.log("Dev mode unsubscribe - really dense fn");
};
if (!DEV_USER_ID) {
unsubscribe = onAuthStateChanged(authStateChangeCallback);
}
return unsubscribe;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useEffect(() => {
const setAuthAsync = async () => {
@@ -47,27 +65,19 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({
setAuthHeader({ key: "user_id", value: DEV_USER_ID });
setIsLoggedIn(true);
setIsLoading(false);
}
}
};
setAuthAsync();
}, [setAuthHeader]);
useEffect(() => {
let unsubscribe: () => void = () => console.log('Dev mode unsubscribe - really dense fn')
if (!DEV_USER_ID) {
unsubscribe = onAuthStateChanged(authStateChangeCallback);
}
return unsubscribe;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
const logOut = async () => {
await AsyncStorage.removeItem("token");
await handleSignOut();
};
return (
<AuthContext.Provider value={{ isLoggedIn, isLoading, user, logOut }}>
<AuthContext.Provider
value={{ isLoggedIn, isLoading, user: contextUser, logOut }}
>
{children}
</AuthContext.Provider>
);