antes de alterar login

This commit is contained in:
2026-05-26 16:40:01 +01:00
parent ef265ceeef
commit 3aa6e5468d
12 changed files with 316 additions and 92 deletions

View File

@@ -33,34 +33,106 @@ export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
return email.replace(/\./g, "_").replace(/@/g, "_at_");
};
// Search for user by UID across all Restaurantes documents
const findUserByUid = async (uid: string, currentUser: FirebaseUser): Promise<RestaurantUser | null> => {
try {
const restaurantsRef = ref(db, "Restaurantes");
const snapshot = await get(restaurantsRef);
if (!snapshot.exists()) return null;
const data = snapshot.val();
for (const key of Object.keys(data)) {
const item = data[key];
if (item?.uid === uid) {
return { ...item, email: item.email || currentUser.email } as RestaurantUser;
}
}
} catch (error) {
console.error("[Auth] Error searching by UID:", error);
}
return null;
};
// Search for user by email across all Restaurantes documents
const findUserByEmail = async (email: string): Promise<RestaurantUser | null> => {
try {
const restaurantsRef = ref(db, "Restaurantes");
const snapshot = await get(restaurantsRef);
if (!snapshot.exists()) return null;
const data = snapshot.val();
for (const key of Object.keys(data)) {
const item = data[key];
if (item?.email === email || item?.establishmentEmail === email || item?.ownerEmail === email) {
return { ...item, email: item.email || email } as RestaurantUser;
}
}
} catch (error) {
console.error("[Auth] Error searching by email:", error);
}
return null;
};
useEffect(() => {
const unsubscribe = onAuthStateChanged(auth, async (currentUser) => {
try {
if (currentUser && currentUser.email) {
setFirebaseUser(currentUser);
let data: RestaurantUser | null = null;
// Strategy 1: Try building document ID from email (website format)
const docId = buildDocumentId(currentUser.email);
const userRef = ref(db, `Restaurantes/${docId}`);
const snapshot = await get(userRef);
let snapshot = await get(userRef);
if (snapshot.exists()) {
const data = snapshot.val() as RestaurantUser;
data = snapshot.val() as RestaurantUser;
}
// Strategy 2: Try with original email as document ID (Android format)
if (!data) {
const originalRef = ref(db, `Restaurantes/${currentUser.email}`);
snapshot = await get(originalRef);
if (snapshot.exists()) {
data = snapshot.val() as RestaurantUser;
}
}
// Strategy 3: Search all Restaurantes documents by UID
if (!data && currentUser.uid) {
data = await findUserByUid(currentUser.uid, currentUser);
}
// Strategy 4: Search all Restaurantes documents by email
if (!data) {
data = await findUserByEmail(currentUser.email);
}
if (data) {
if (data.accountType === "ESTABELECIMENTO") {
setUser(data);
} else {
console.warn("User is not a restaurant");
console.warn("[Auth] User is not a restaurant account type:", data.accountType);
setUser(null);
await signOut(auth);
}
} else {
console.warn("User record not found in Restaurantes");
// User exists in Auth but not in database — possible orphaned account
console.warn("[Auth] User record not found in Restaurantes for:", currentUser.email);
console.warn("[Auth] UID:", currentUser.uid);
setUser(null);
}
} else {
// No Firebase user — clear state
setFirebaseUser(null);
setUser(null);
}
} catch (error) {
console.error("Auth initialization error:", error);
console.error("[Auth] Error in onAuthStateChanged:", error);
// On error, clear user state to prevent broken state
setUser(null);
setFirebaseUser(null);
} finally {
setLoading(false);
}