-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
105 additions
and
0 deletions.
There are no files selected for viewing
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,105 @@ | ||
// import React, { | ||
// createContext, | ||
// useContext, | ||
// useEffect, | ||
// useState, | ||
// ReactNode, | ||
// } from 'react'; | ||
// import { getAuth, onAuthStateChanged, User } from 'firebase/auth'; | ||
|
||
// interface AuthContextProps { | ||
// currentUser: User | null; | ||
// } | ||
|
||
// interface AuthProviderProps { | ||
// children: ReactNode; | ||
// } | ||
|
||
// const AuthContext = createContext<AuthContextProps | undefined>(undefined); | ||
|
||
// function useAuth() { | ||
// const context = useContext(AuthContext); | ||
|
||
// if (!context) { | ||
// throw new Error('useAuth debe ser utilizado dentro de un AuthProvider'); | ||
// } | ||
|
||
// return context; | ||
// } | ||
|
||
// function AuthProvider({ children }: AuthProviderProps) { | ||
// const [currentUser, setCurrentUser] = useState<User | null>(null); | ||
// const auth = getAuth(); | ||
|
||
// useEffect(() => { | ||
// const unsubscribe = onAuthStateChanged(auth, (user) => { | ||
// setCurrentUser(user); | ||
// }); | ||
|
||
// // Limpia el evento cuando el componente se desmonta | ||
// return () => unsubscribe(); | ||
// }, [auth]); | ||
|
||
// const value: AuthContextProps = { | ||
// currentUser, | ||
// }; | ||
|
||
// return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>; | ||
// } | ||
|
||
// export { useAuth, AuthProvider }; | ||
|
||
import React, { | ||
createContext, | ||
useContext, | ||
useEffect, | ||
useState, | ||
ReactNode, | ||
} from 'react'; | ||
import { getAuth, onAuthStateChanged, User } from 'firebase/auth'; | ||
|
||
interface AuthContextProps { | ||
currentUser: User | null; | ||
isAuthenticated: boolean; // Nueva propiedad para indicar si el usuario está autenticado | ||
} | ||
|
||
interface AuthProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
const AuthContext = createContext<AuthContextProps | undefined>(undefined); | ||
|
||
function useAuth() { | ||
const context = useContext(AuthContext); | ||
|
||
if (!context) { | ||
throw new Error('useAuth debe ser utilizado dentro de un AuthProvider'); | ||
} | ||
|
||
return context; | ||
} | ||
|
||
function AuthProvider({ children }: AuthProviderProps) { | ||
const [currentUser, setCurrentUser] = useState<User | null>(null); | ||
const [isAuthenticated, setIsAuthenticated] = useState(false); // Nuevo estado para indicar si el usuario está autenticado | ||
const auth = getAuth(); | ||
|
||
useEffect(() => { | ||
const unsubscribe = onAuthStateChanged(auth, (user) => { | ||
setCurrentUser(user); | ||
setIsAuthenticated(!!user); // Actualiza el estado de autenticación | ||
}); | ||
|
||
// Limpia el evento cuando el componente se desmonta | ||
return () => unsubscribe(); | ||
}, [auth]); | ||
|
||
const value: AuthContextProps = { | ||
currentUser, | ||
isAuthenticated, | ||
}; | ||
|
||
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>; | ||
} | ||
|
||
export { useAuth, AuthProvider }; |
51710c0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
xbu – ./
xbu-git-main-franqsanz.vercel.app
xbu.vercel.app
xbu-franqsanz.vercel.app