-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(firebase): Extract hook into single hook file, auth-related …
…functions into auth.js - Moved authentication-related logic into a dedicated `auth.js` file. - Extracted `useAuthState` into its own hook file. - Deleted the old `firebase.js` file and replaced it with `firebaseConfig.js` for Firebase initialization. - Refactored components to align with the updated hook and utils changes.
- Loading branch information
Showing
10 changed files
with
89 additions
and
95 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
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,23 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
import { onAuthStateChanged } from 'firebase/auth'; | ||
|
||
import { auth } from '../utils/firebaseConfig'; | ||
|
||
// Hook to get the current user | ||
export const useAuthState = () => { | ||
const [user, setUser] = useState(null); | ||
const [error, setError] = useState(null); | ||
|
||
useEffect(() => { | ||
const unsubscribe = onAuthStateChanged( | ||
auth, | ||
(user) => setUser(user), | ||
(error) => setError(error), | ||
); | ||
|
||
return () => unsubscribe(); // cleanup on unmount | ||
}, []); | ||
|
||
return [user, error]; | ||
}; |
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,39 @@ | ||
import { GoogleAuthProvider, signInWithPopup, signOut } from 'firebase/auth'; | ||
|
||
import { auth } from './firebaseConfig'; | ||
import { checkUserProfile } from './firestore'; | ||
|
||
const provider = new GoogleAuthProvider(); | ||
|
||
// Google Sign-In | ||
const signInWithGoogle = async () => { | ||
try { | ||
const result = await signInWithPopup(auth, provider); | ||
return result.user; | ||
} catch (error) { | ||
console.error('Error during sign-in:', error); | ||
return null; | ||
} | ||
}; | ||
|
||
// Handle Sign-In | ||
// Return true: user already exists in the database | ||
export const handleSignIn = async () => { | ||
const user = await signInWithGoogle(); | ||
let alreadyExist = true; | ||
if (user) { | ||
alreadyExist = await checkUserProfile(user); | ||
} | ||
return alreadyExist; | ||
}; | ||
|
||
// Handle Sign-Out | ||
export const handleSignOut = async (navigate) => { | ||
try { | ||
await signOut(auth); | ||
console.log('Sign out successful'); | ||
navigate('/'); | ||
} catch (error) { | ||
console.error('Error during sign-out:', error); | ||
} | ||
}; |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { initializeApp } from 'firebase/app'; | ||
import { getAuth } from 'firebase/auth'; | ||
import { getFirestore } from 'firebase/firestore'; | ||
|
||
const firebaseConfig = { | ||
apiKey: 'AIzaSyAtUHyFNDDtuPVwmH9mNCXa6o2qE_kF6OA', | ||
authDomain: 'studybuddy-8086e.firebaseapp.com', | ||
projectId: 'studybuddy-8086e', | ||
storageBucket: 'studybuddy-8086e.appspot.com', | ||
messagingSenderId: '677938268288', | ||
appId: '1:677938268288:web:b74725ffd461455c76be65', | ||
measurementId: 'G-EKR2SD2LE8', | ||
}; | ||
|
||
// Initialize Firebase | ||
const app = initializeApp(firebaseConfig); | ||
|
||
export const db = getFirestore(app); | ||
export const auth = getAuth(app); |
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