Skip to content

Commit

Permalink
adding src folder modifying auth stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
rbrtm984 committed Feb 12, 2024
1 parent 4209d3e commit 61bf0de
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import app from "./firebaseConfig";
import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword, GoogleAuthProvider, signInWithCredential } from "firebase/auth";
import * as Google from "expo-auth-session/providers/google";
import { ResponseType } from "expo-auth-session";
import { AuthProvider } from "./src/contexts/AuthContext";

import { StatusBar } from "expo-status-bar";

Expand Down
32 changes: 32 additions & 0 deletions src/contexts/AuthContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// src/contexts/AuthContext.ts
import React, { createContext, useContext, ReactNode, useState, useEffect } from 'react';
import { getAuth, onAuthStateChanged, User } from 'firebase/auth';

interface AuthContextType {
currentUser: User | null;
}

const AuthContext = createContext<AuthContextType>({ currentUser: null });

interface AuthProviderProps {
children: ReactNode;
}

export const AuthProvider = ({ children }: AuthProviderProps) => {
const [currentUser, setCurrentUser] = useState<User | null>(null);

useEffect(() => {
const auth = getAuth();
const unsubscribe = onAuthStateChanged(auth, (user) => {
setCurrentUser(user);
});

return unsubscribe;
}, []);

return <AuthContext.Provider value={{ currentUser }}>
{children}
</AuthContext.Provider>;
};

export const useAuth = () => useContext(AuthContext);

0 comments on commit 61bf0de

Please sign in to comment.