Skip to content

Commit

Permalink
fix: added file
Browse files Browse the repository at this point in the history
  • Loading branch information
Franqsanz committed Dec 22, 2023
1 parent 3e13fb9 commit 51710c0
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions src/store/AuthContext.tsx
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 };

1 comment on commit 51710c0

@vercel
Copy link

@vercel vercel bot commented on 51710c0 Dec 22, 2023

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

Please sign in to comment.