class AuthCubit extends Cubit<AuthState> {
AuthCubit() : super(AuthState());
FirebaseAuth auth = FirebaseAuth.instance;
FirebaseFirestore firestore = FirebaseFirestore.instance;
signUpWIthPhoneNumber(
String phoneNumber, String userName, BuildContext context) async {
emit(state.copyWith(
phoneNumber: phoneNumber,
userName: userName.isEmpty ? state.userName : userName,
isLoading: true));
await auth.verifyPhoneNumber(
phoneNumber: phoneNumber,
verificationCompleted: (PhoneAuthCredential credential) async {
// Sign the user in (or link) with the auto-generated credential
UserCredential userCredential =
await auth.signInWithCredential(credential);
emit(state.copyWith(userCredential: userCredential, isLoading: false));
},
timeout: Duration(seconds: 60),
codeAutoRetrievalTimeout: (String verificationId) {},
codeSent: (String verificationId, int? forceResendingToken) async {
// Create a PhoneAuthCredential with the code
emit(state.copyWith(verificationId: verificationId, isLoading: false));
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CodeVerificationPage(),
),
);
},
verificationFailed: (FirebaseAuthException error) {
emit(state.copyWith(isLoading: false));
print(error);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('$error'),
),
);
},
);
}
signInWithCridentials(String sms) async {
PhoneAuthCredential _credential = PhoneAuthProvider.credential(
verificationId: state.verificationId, smsCode: sms);
UserCredential userCredential =
await auth.signInWithCredential(_credential);
await updateUsername();
createUserInFirestore();
emit(state.copyWith(userCredential: userCredential));
}
updateUsername() async {
final user = auth.currentUser;
await user?.updateDisplayName(
(state.userName.isEmpty) ? 'New User' : state.userName);
}
logout() async {
auth.signOut();
emit(state.copyWith(userCredential: null));
}
createUserInFirestore() async {
User? user = auth.currentUser;
DocumentReference<Map<String, dynamic>> firestoreUsers =
firestore.collection('users').doc(user?.uid);
var aUser = await firestoreUsers.get();
if (!aUser.exists) {
await firestore.collection('users').doc(user?.phoneNumber).set({
'username': user?.displayName ?? state.userName,
'createdAt': Timestamp.now().millisecondsSinceEpoch,
'phoneNumber': user?.phoneNumber,
'auth_token': user?.uid,
});
} else {
print('This user already sigined in');
}
}
resendCode(BuildContext context) async {
await auth.verifyPhoneNumber(
phoneNumber: state.phoneNumber,
verificationCompleted: (PhoneAuthCredential credential) async {
// Sign the user in (or link) with the auto-generated credential
UserCredential userCredential =
await auth.signInWithCredential(credential);
emit(state.copyWith(userCredential: userCredential, isLoading: false));
},
timeout: Duration(seconds: 60),
codeAutoRetrievalTimeout: (String verificationId) {},
codeSent: (String verificationId, int? forceResendingToken) async {
// Create a PhoneAuthCredential with the code
emit(state.copyWith(verificationId: verificationId, isLoading: false));
},
verificationFailed: (FirebaseAuthException error) {
emit(state.copyWith(isLoading: false));
print(error);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('$error'),
),
);
},
);
}
setNewCountry(String dialCode, String initialCountry) {
emit(state.copyWith(dialCode: dialCode, initialCountry: initialCountry));
}
}
Bug report
Describe the bug
I have integrated firebase to flutter app using flutterfire cli. I have enabled only phone auth, but when i tried to login with phone numer it's returning error. like: [firebase_auth/invalid-client-id] Client does not match API key (IOS)
i added CFBundleURLTypes to info.plist
i enabled push notifications
i enabled background modes
i uploaded APNs key to firebase console
but nothing helped.
Here is how i use phone auth using cubit:
Expected behavior
It should login with phone numbers
Flutter doctor
Run
flutter doctorand paste the output below:Click To Expand
Flutter dependencies
Run
flutter pub deps -- --style=compactand paste the output below:Click To Expand