Save all usernames created in a separate list in firestore, also save the username on the user.
[ ] New list in firestore
[ ] Update registration function
[ ] Update Login & Registration form
[ ] Add username to user in storefront
`// Check if username is available
function isUsernameAvailable(username) {
// Query Firestore to check if the username exists
return firebase.firestore().collection('users').where('username', '==', username).get()
.then((querySnapshot) => {
return querySnapshot.empty; // Return true if no matching usernames found
});
}
// Register new user with email and username
function registerUser(email, password, username) {
return isUsernameAvailable(username)
.then((isAvailable) => {
if (!isAvailable) {
throw new Error('Username is already taken. Please choose a different one.');
}
// Create user with email and password
return firebase.auth().createUserWithEmailAndPassword(email, password);
})
.then((userCredential) => {
// Add user data to Firestore
const user = userCredential.user;
return firebase.firestore().collection('users').doc(user.uid).set({
email: user.email,
username: username
});
});
}`
Save all usernames created in a separate list in firestore, also save the username on the user.
`// Check if username is available function isUsernameAvailable(username) { // Query Firestore to check if the username exists return firebase.firestore().collection('users').where('username', '==', username).get() .then((querySnapshot) => { return querySnapshot.empty; // Return true if no matching usernames found }); }
// Register new user with email and username function registerUser(email, password, username) { return isUsernameAvailable(username) .then((isAvailable) => { if (!isAvailable) { throw new Error('Username is already taken. Please choose a different one.'); } // Create user with email and password return firebase.auth().createUserWithEmailAndPassword(email, password); }) .then((userCredential) => { // Add user data to Firestore const user = userCredential.user; return firebase.firestore().collection('users').doc(user.uid).set({ email: user.email, username: username }); }); }`