Check checkUserExists Both and Return All Matches Angular

 async function checkUserExists(email, phone) {

  try {

    const results = { exists: false, byEmail: false, byPhone: false };

    

    // Check by email if provided

    if (email) {

      const emailQuery = query(

        collection(db, 'users'),

        where('email', '==', email),

        limit(1)

      );

      const emailSnapshot = await getDocs(emailQuery);

      if (!emailSnapshot.empty) {

        results.exists = true;

        results.byEmail = true;

      }

    }


    // Check by phone if provided (always checks both)

    if (phone) {

      const phoneQuery = query(

        collection(db, 'users'),

        where('phone', '==', phone),

        limit(1)

      );

      const phoneSnapshot = await getDocs(phoneQuery);

      if (!phoneSnapshot.empty) {

        results.exists = true;

        results.byPhone = true;

      }

    }


    return results;

  } catch (error) {

    console.error("Error checking user existence:", error);

    return { exists: false, byEmail: false, byPhone: false, error: error.message };

  }

}


// Usage

const result = await checkUserExists('test@example.com', '+1234567890');

console.log(result);

// Possible outputs:

// { exists: true, byEmail: true, byPhone: false } - email exists

// { exists: true, byEmail: false, byPhone: true } - phone exists

// { exists: true, byEmail: true, byPhone: true } - both exist

// { exists: false, byEmail: false, byPhone: false } - neither exists

No comments:

Post a Comment

check UserExistsParallel Parallel Checks (Faster)

 async function checkUserExistsParallel(email, phone) {   try {     const checks = [];          if (email) {       checks.push(         getD...

Best for you