imprex92 / dark-traveling-marshmallow

Traveling project
https://dark-traveling-marshmallow.web.app/
2 stars 0 forks source link

Implement the use of usernames #188

Open imprex92 opened 3 months ago

imprex92 commented 3 months ago

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 }); }); }`