- AuthRepository.signInWithApple() via eingebautem AppleAuthProvider (nativ auf iOS, Web-Flow über Firebase-Handler auf Android, ohne Zusatzpaket) - Haushalts-Bootstrap in gemeinsamen Helper _bootstrapHouseholdIfNeeded ausgelagert; beim ersten Apple-Login wird Profil + Haushalt idempotent angelegt - LoginScreen: "oder"-Trenner + "Mit Apple anmelden"-Button, Abbruch ohne Fehlermeldung - l10n: signInWithApple, orDivider, authErrorAppleFailed - iOS: Entitlement com.apple.developer.applesignin ergänzt - Doku: firebase-einrichtung.md Schritt 7 (Apple-Login) mit Portal-/Console-Anleitung Noch offen: Apple-Portal (Services-ID, Sign-in-Key), Firebase-Console-Provider, Xcode-Capability — danach Ende-zu-Ende auf iOS+Android testen. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
81 lines
2.9 KiB
Dart
81 lines
2.9 KiB
Dart
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:firebase_auth/firebase_auth.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import '../../../core/firebase/firebase_providers.dart';
|
|
|
|
/// Anmeldung, Registrierung und der Haushalts-Bootstrap.
|
|
class AuthRepository {
|
|
AuthRepository(this._auth, this._firestore);
|
|
|
|
final FirebaseAuth _auth;
|
|
final FirebaseFirestore _firestore;
|
|
|
|
Future<void> signIn({required String email, required String password}) async {
|
|
await _auth.signInWithEmailAndPassword(email: email, password: password);
|
|
}
|
|
|
|
/// Registrierung: legt den Auth-Nutzer an und danach Profil + Haushalt.
|
|
Future<void> signUp({required String email, required String password}) async {
|
|
final credential = await _auth.createUserWithEmailAndPassword(
|
|
email: email,
|
|
password: password,
|
|
);
|
|
await _bootstrapHouseholdIfNeeded(credential.user!.uid, email);
|
|
}
|
|
|
|
/// Anmeldung mit Apple. Nativer Dialog auf iOS, Web-Flow (über den
|
|
/// Firebase-Auth-Handler) auf Android — beides über den eingebauten
|
|
/// [AppleAuthProvider], daher ohne Zusatz-Paket. Beim **ersten** Login
|
|
/// existiert noch kein Firestore-Profil, deshalb hängt der Haushalts-
|
|
/// Bootstrap hier dran (Apple legt nur den Auth-Nutzer an).
|
|
Future<void> signInWithApple() async {
|
|
final provider = AppleAuthProvider()
|
|
..addScope('email')
|
|
..addScope('name');
|
|
final credential = await _auth.signInWithProvider(provider);
|
|
final user = credential.user!;
|
|
await _bootstrapHouseholdIfNeeded(user.uid, user.email);
|
|
}
|
|
|
|
/// Legt Profil-Dokument und einen eigenen Haushalt an, falls für [uid]
|
|
/// noch keins existiert. Idempotent — mehrfaches Anmelden mit Apple oder
|
|
/// eine erneute Registrierung überschreibt einen bestehenden Haushalt nie.
|
|
/// [email] kann bei „E-Mail verbergen“ die Apple-Relay-Adresse sein.
|
|
Future<void> _bootstrapHouseholdIfNeeded(String uid, String? email) async {
|
|
final userRef = _firestore.collection('users').doc(uid);
|
|
if ((await userRef.get()).exists) return;
|
|
|
|
final safeEmail = email ?? '';
|
|
final householdRef = _firestore.collection('households').doc();
|
|
final batch = _firestore.batch();
|
|
batch.set(householdRef, {
|
|
'name': 'Mein Haushalt',
|
|
'ownerUid': uid,
|
|
'memberUids': [uid],
|
|
'members': {
|
|
uid: {
|
|
'role': 'member',
|
|
'email': safeEmail,
|
|
'joinedAt': FieldValue.serverTimestamp(),
|
|
},
|
|
},
|
|
'createdAt': FieldValue.serverTimestamp(),
|
|
});
|
|
batch.set(userRef, {
|
|
'email': safeEmail,
|
|
'householdId': householdRef.id,
|
|
'createdAt': FieldValue.serverTimestamp(),
|
|
});
|
|
await batch.commit();
|
|
}
|
|
|
|
Future<void> signOut() => _auth.signOut();
|
|
}
|
|
|
|
final authRepositoryProvider = Provider<AuthRepository>((ref) {
|
|
return AuthRepository(
|
|
ref.watch(firebaseAuthProvider),
|
|
ref.watch(firestoreProvider),
|
|
);
|
|
});
|