diff --git a/.eslintrc.js b/.eslintrc.js index c278541..bf8aa8d 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -30,7 +30,7 @@ module.exports = { rules: { // Best Practices eqeqeq: ["error", "always"], // Enforce '===' instead of '==' - curly: "error", // Require curly braces for all control statements + curly: ["error", "multi-line", "consistent"], // Require curly braces for all control statements "no-unused-vars": "warn", // Warn about variables that are declared but not used // React Specific diff --git a/src/auth/firebase-auth.tsx b/src/auth/firebase-auth.tsx index 35e37c4..d545816 100644 --- a/src/auth/firebase-auth.tsx +++ b/src/auth/firebase-auth.tsx @@ -38,6 +38,8 @@ export const onAuthStateChanged = ( return auth().onAuthStateChanged(callback); }; +export const currentUser = () => auth().currentUser; + export const getCurrentUserToken = async (): Promise => { const user = auth().currentUser; if (user) { diff --git a/src/auth/index.ts b/src/auth/index.ts index 89b62ac..e766cfb 100644 --- a/src/auth/index.ts +++ b/src/auth/index.ts @@ -1,6 +1,6 @@ import { confirmCode, - getCurrentUserToken, + currentUser, handleSignInWithPhoneNumber, handleSignOut, onAuthStateChanged, @@ -8,7 +8,7 @@ import { export { confirmCode, - getCurrentUserToken, + currentUser, handleSignInWithPhoneNumber, handleSignOut, onAuthStateChanged, diff --git a/src/context/auth-context.tsx b/src/context/auth-context.tsx index ba451ee..7db6f60 100644 --- a/src/context/auth-context.tsx +++ b/src/context/auth-context.tsx @@ -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(null); + const [contextUser, setContextUser] = useState( + null, + ); const [isLoggedIn, setIsLoggedIn] = useState(false); const [isLoading, setIsLoading] = useState(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 ( - + {children} ); diff --git a/src/lib/loading/index.tsx b/src/lib/loading/index.tsx index ebf72dc..5854611 100644 --- a/src/lib/loading/index.tsx +++ b/src/lib/loading/index.tsx @@ -1,36 +1,39 @@ -import React, { createContext, useEffect, useState } from 'react'; -import { ActivityIndicator, StyleSheet, View } from 'react-native'; +import React, { createContext, useEffect, useState } from "react"; +import { ActivityIndicator, StyleSheet, View } from "react-native"; -import { colors } from '../../styles'; -import AsyncStorage from '@react-native-async-storage/async-storage'; +import AsyncStorage from "@react-native-async-storage/async-storage"; +import { colors } from "../../styles"; const LoadingContext = createContext(false); export default function Loading({ children }) { - const [loading, setLoading] = useState(true); + const [loading, setLoading] = useState(true); - useEffect(() => { - const checkHasToken = async () => { - const hasToken = await AsyncStorage.getItem('token'); - // needs to connect to app state - return hasToken - } + useEffect(() => { + const checkHasToken = async () => { + const hasToken = await AsyncStorage.getItem("token"); + // needs to connect to app state + return hasToken; + }; - checkHasToken() - .then(() => setLoading(false)) - }, []); + checkHasToken().then(() => setLoading(false)); + }, []); - if (!loading) { - return {children}; - } + if (!loading) { + return ( + + {children} + + ); + } - return ( - - - - ); + return ( + + + + ); } const s = StyleSheet.create({ - container: { flex: 1, justifyContent: 'center', alignItems: 'center' } -}); \ No newline at end of file + container: { flex: 1, justifyContent: "center", alignItems: "center" }, +});