-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created AuthContext to share current login user's info
- Loading branch information
Showing
13 changed files
with
600 additions
and
143 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { | ||
useContext, | ||
createContext, | ||
useEffect, | ||
useState, | ||
FC, | ||
ReactNode, | ||
} from "react"; | ||
import { onAuthStateChanged, User } from "firebase/auth"; | ||
import { auth, db } from "../config/firebase"; | ||
import { doc, getDoc } from "firebase/firestore"; | ||
|
||
// Define the type for the UserContext | ||
type UserContextType = { | ||
user: User | null; | ||
isConsumer: boolean; | ||
}; | ||
|
||
// Create a context with a default value of null | ||
const AuthContext = createContext<UserContextType | null>(null); | ||
|
||
// AuthContextProvider component to wrap around the app | ||
export const AuthContextProvider: FC<{ children: ReactNode }> = ({ | ||
children, | ||
}) => { | ||
const [user, setUser] = useState<User | null>(null); | ||
const [isConsumer, setIsConsumer] = useState<boolean>(false); | ||
|
||
// useEffect to listen for authentication state changes | ||
useEffect(() => { | ||
const unsubscribe = onAuthStateChanged( | ||
auth, | ||
async (currentUser) => { | ||
setUser(currentUser); // Set the current user in state | ||
if (currentUser) { | ||
const docRef = doc(db, "consumer", currentUser.uid); | ||
const docSnap = await getDoc(docRef); | ||
if (docSnap.exists()) { | ||
setIsConsumer(true); | ||
} | ||
} | ||
}, | ||
(error) => { | ||
console.error("Auth state change error: ", error); // Log any errors | ||
} | ||
); | ||
|
||
// Cleanup subscription on component unmount | ||
return () => { | ||
unsubscribe(); | ||
}; | ||
}, []); | ||
|
||
// Provide the user state to child components | ||
return ( | ||
<AuthContext.Provider value={{ user, isConsumer }}> | ||
{children} | ||
</AuthContext.Provider> | ||
); | ||
}; | ||
|
||
// Custom hook to use the AuthContext | ||
export const useAuth = (): UserContextType => { | ||
const context = useContext(AuthContext); | ||
|
||
// Throw an error if the hook is used outside of AuthProvider | ||
if (!context) { | ||
throw new Error("useAuth must be used within an AuthProvider"); | ||
} | ||
|
||
return context; // Return the context value | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.