【发布时间】:2021-10-17 18:44:25
【问题描述】:
我想对 Flutter 应用使用 Firebase Phone 身份验证。我有一个 AuthService 类,我需要公开 getter codeSent 来更改 UI。来自 Riverpod 官方网站的第三方示例正在使用其他软件包,并且不是很清楚。那么在这种情况下如何使用 Riverod 呢?
import 'dart:async';
import 'package:firebase_auth/firebase_auth.dart';
class AuthService {
final FirebaseAuth _auth = FirebaseAuth.instance;
String _verificationId = '';
int? _resendToken;
bool _codeSent = false;
Stream<User?> get user {
return _auth.authStateChanges();
}
get currentUser {
return _auth.currentUser;
}
Future verifyPhoneNumber(String phoneNumber) async {
await _auth.verifyPhoneNumber(
verificationCompleted: (PhoneAuthCredential credential) async {
await _auth.signInWithCredential(credential);
},
verificationFailed: (FirebaseAuthException e) {
if (e.code == 'invalid-phone-number') {
print('The provided phone number is not valid.');
}
},
codeSent: (String verificationId, int? resendToken) {
_verificationId = verificationId;
_resendToken = resendToken;
_codeSent = true;
},
codeAutoRetrievalTimeout: (String verificationId) {
_verificationId = verificationId;
});
}
Future signWithOtp(String otp) async {
PhoneAuthCredential credential = PhoneAuthProvider.credential(
verificationId: _verificationId, smsCode: otp);
await _auth.signInWithCredential(credential);
}
Future signOut() async {
try {
return await _auth.signOut();
} catch (error) {
print(error.toString());
return null;
}
}
}
【问题讨论】:
标签: firebase flutter dart firebase-authentication riverpod