-
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.
- Loading branch information
1 parent
512c720
commit 9024003
Showing
3 changed files
with
33 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,35 @@ | ||
import { | ||
getAuth, | ||
signInWithPopup, | ||
GoogleAuthProvider, | ||
signInWithRedirect, | ||
getRedirectResult, | ||
signOut as firebaseSignOut, | ||
signInAnonymously as firebaseSignInAnonymously | ||
} from 'firebase/auth'; | ||
import { getAuth, signInWithPopup, GoogleAuthProvider, signInWithRedirect, getRedirectResult, signOut as firebaseSignOut } from 'firebase/auth'; // Added getRedirectResult and firebaseSignOut | ||
import app from './firebaseConfig'; | ||
|
||
const provider = new GoogleAuthProvider(); | ||
export const auth = getAuth(app); // Get and export the authentication object | ||
const auth = getAuth(app); // Pass the Firebase app instance to getAuth | ||
|
||
// Initiates sign-in with Google using redirect | ||
export const signInWithGoogle = () => { | ||
// Initiates sign-in with redirect | ||
signInWithRedirect(auth, provider); | ||
}; | ||
|
||
// Handles the results from the redirect sign-in | ||
export const handleRedirectResult = async () => { | ||
try { | ||
const result = await getRedirectResult(auth); | ||
if (result && result.user) { | ||
return result.user; // Returns the user object from Firebase Auth | ||
} else { | ||
// Handle the case where no user is returned | ||
console.log("No user from redirect, likely not logged in"); | ||
return null; | ||
if (result) { | ||
// This gives you a Google Access Token. You can use it to access Google APIs. | ||
// The signed-in user info is now available | ||
return result.user; // Return the user object for further processing | ||
} | ||
} catch (error) { | ||
// Handle Errors here. | ||
console.error("Error handling redirect result:", error); | ||
throw error; | ||
throw error; // Rethrow or handle errors appropriately | ||
} | ||
}; | ||
|
||
// Signs out the current user | ||
export const signOut = async () => { | ||
try { | ||
await firebaseSignOut(auth); | ||
console.log("Sign-out successful."); | ||
} catch (error) { | ||
export const signOut = () => { | ||
firebaseSignOut(auth).then(() => { | ||
// Sign-out successful. | ||
// Update UI or redirect as needed | ||
}).catch((error) => { | ||
// An error happened during sign-out. | ||
console.error("Error during sign-out:", error); | ||
} | ||
}; | ||
|
||
// Signs in a user anonymously | ||
export const signInAnonymously = async () => { | ||
try { | ||
const result = await firebaseSignInAnonymously(auth); | ||
return result; // Assuming you want to return the result object | ||
} catch (error) { | ||
console.error("Error signing in anonymously:", error); | ||
throw 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 |
---|---|---|
|
@@ -62,4 +62,4 @@ export const getUserData = async (userId) => { | |
console.log("No such user!"); | ||
return null; | ||
} | ||
}; | ||
}; |