auth phone and re auth phone
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
|
||||
class PhoneVerificationService {
|
||||
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
|
||||
|
||||
Stream<PhoneAuthEvent> verifyPhoneNumber(String phoneNumber) async* {
|
||||
final StreamController<PhoneAuthEvent> phoneAuthController =
|
||||
StreamController<PhoneAuthEvent>();
|
||||
|
||||
_firebaseAuth.verifyPhoneNumber(
|
||||
phoneNumber: phoneNumber,
|
||||
timeout: const Duration(seconds: 60),
|
||||
verificationCompleted: (AuthCredential authCredential) async {
|
||||
phoneAuthController
|
||||
.add(PhoneAuthEvent.verificationCompleted(authCredential));
|
||||
},
|
||||
verificationFailed: (FirebaseAuthException authException) async {
|
||||
phoneAuthController
|
||||
.add(PhoneAuthEvent.verificationFailed(authException));
|
||||
phoneAuthController.close();
|
||||
},
|
||||
codeAutoRetrievalTimeout: (String verificationId) async {
|
||||
phoneAuthController
|
||||
.add(PhoneAuthEvent.codeAutoRetrievalTimeout(verificationId));
|
||||
},
|
||||
codeSent: (String verificationId, int? resendToken) async {
|
||||
phoneAuthController
|
||||
.add(PhoneAuthEvent.codeSent(verificationId, resendToken));
|
||||
},
|
||||
);
|
||||
|
||||
await for (PhoneAuthEvent event in phoneAuthController.stream) {
|
||||
yield event;
|
||||
if (event.type == PhoneAuthEventType.verificationFailed) {
|
||||
await phoneAuthController.close();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum PhoneAuthEventType {
|
||||
verificationCompleted,
|
||||
verificationFailed,
|
||||
codeAutoRetrievalTimeout,
|
||||
codeSent,
|
||||
}
|
||||
|
||||
class PhoneAuthEvent {
|
||||
final PhoneAuthEventType type;
|
||||
final dynamic data;
|
||||
|
||||
PhoneAuthEvent(this.type, this.data);
|
||||
|
||||
static PhoneAuthEvent verificationCompleted(AuthCredential authCredential) {
|
||||
return PhoneAuthEvent(
|
||||
PhoneAuthEventType.verificationCompleted, authCredential);
|
||||
}
|
||||
|
||||
static PhoneAuthEvent verificationFailed(
|
||||
FirebaseAuthException authException) {
|
||||
return PhoneAuthEvent(PhoneAuthEventType.verificationFailed, authException);
|
||||
}
|
||||
|
||||
static PhoneAuthEvent codeAutoRetrievalTimeout(String verificationId) {
|
||||
return PhoneAuthEvent(
|
||||
PhoneAuthEventType.codeAutoRetrievalTimeout, verificationId);
|
||||
}
|
||||
|
||||
static PhoneAuthEvent codeSent(String verificationId, int? resendToken) {
|
||||
return PhoneAuthEvent(PhoneAuthEventType.codeSent,
|
||||
{'verificationId': verificationId, 'resendToken': resendToken});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user