-
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.
Added New workflow to clean Anon users, as well as updated sign in page
To allow Anon users to sign in instead.
- Loading branch information
1 parent
b0f06b2
commit 4d90c62
Showing
5 changed files
with
125 additions
and
39 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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import admin from 'firebase-admin'; | ||
import dotenv from 'dotenv'; | ||
|
||
dotenv.config(); | ||
|
||
admin.initializeApp({ | ||
credential: admin.credential.cert({ | ||
projectId: process.env.FIREBASE_PROJECT_ID, | ||
clientEmail: process.env.FIREBASE_CLIENT_EMAIL, | ||
privateKey: process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n') | ||
}) | ||
}); | ||
|
||
const deleteAnonymousUsers = async () => { | ||
let nextPageToken; | ||
do { | ||
const listUsersResult = await admin.auth().listUsers(1000, nextPageToken); | ||
nextPageToken = listUsersResult.pageToken; | ||
|
||
const uidsToDelete = listUsersResult.users | ||
.filter(user => user.providerData.length === 0) // Filter anonymous users | ||
.map(user => user.uid); | ||
|
||
if (uidsToDelete.length > 0) { | ||
await Promise.all(uidsToDelete.map(uid => admin.auth().deleteUser(uid))); | ||
console.log(`Deleted ${uidsToDelete.length} anonymous users`); | ||
} | ||
} while (nextPageToken); | ||
}; | ||
|
||
deleteAnonymousUsers().catch(console.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
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,35 +1,56 @@ | ||
import { getAuth, signInWithPopup, GoogleAuthProvider, signInWithRedirect, getRedirectResult, signOut as firebaseSignOut } from 'firebase/auth'; // Added getRedirectResult and firebaseSignOut | ||
import { | ||
getAuth, | ||
signInWithPopup, | ||
GoogleAuthProvider, | ||
signInWithRedirect, | ||
getRedirectResult, | ||
signOut as firebaseSignOut, | ||
signInAnonymously as firebaseSignInAnonymously | ||
} from 'firebase/auth'; | ||
import app from './firebaseConfig'; | ||
|
||
const provider = new GoogleAuthProvider(); | ||
const auth = getAuth(app); // Pass the Firebase app instance to getAuth | ||
export const auth = getAuth(app); // Get and export the authentication object | ||
|
||
// 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) { | ||
// 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 | ||
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; | ||
} | ||
} catch (error) { | ||
// Handle Errors here. | ||
console.error("Error handling redirect result:", error); | ||
throw error; // Rethrow or handle errors appropriately | ||
throw error; | ||
} | ||
}; | ||
|
||
export const signOut = () => { | ||
firebaseSignOut(auth).then(() => { | ||
// Sign-out successful. | ||
// Update UI or redirect as needed | ||
}).catch((error) => { | ||
// An error happened during sign-out. | ||
// Signs out the current user | ||
export const signOut = async () => { | ||
try { | ||
await firebaseSignOut(auth); | ||
console.log("Sign-out successful."); | ||
} catch (error) { | ||
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