[英]send verification mail in flutter/dart development
我是 flutter 和 dart 移動應用程序開發的新手。 如何在 flutter/dart 開發中實現忘記密碼和發送驗證郵件,或者有什么方法可以實現發送郵件。
我認為沒有任何方法可以從您的 Flutter 應用程序發送電子郵件。 這是我肯定會在后端服務器上實現的東西。
我會在flutter中實現一個“忘記密碼”按鈕,它會觸發對后端的http調用,然后觸發密碼生成和電子郵件發送。
在此處閱讀有關通過互聯網撥打電話的更多信息:[ https://flutter.dev/docs/cookbook/networking/fetch-data] [1 ]
編輯:該鏈接與您如何可以從Internet發出請求和接受響應(在這種情況下忘記密碼時在服務器中發出請求)相關。
因此,您必須創建一個后端服務來接受來自客戶的呼叫,在這種情況下,您的客戶是撲朔迷離的應用程序,在這里我無法提供使用代碼的方法,您應該了解更多有關這樣做的信息。 在創建服務器並實現了接受和驗證請求的邏輯之后,服務器可以返回帶有鏈接的請求以重置密碼,然后您可以在flutter應用程序中呈現響應,因此可以發出另一個請求新的憑據。 另一種方法是通過電子郵件將鏈接(請求)發送給客戶端(他/她注冊時提供的電子郵件用戶),然后客戶端可以相應地更改密碼。
希望這會有所幫助。
是的,有一些方法。 最常見的是使用 firebase 作為處理這些請求的后端服務器。
你可以這樣做
將這些包添加到您的 flutter 應用程序 pubspec.yaml 文件中
// latest version
firebase_core: ^1.17.0
firebase_auth: ^3.3.18
一旦您在發出請求之前完成了必要的邏輯,請調用此 function
sendResetEmail(String email, BuildContext context) async {
final FirebaseAuth _auth = FirebaseAuth.instance;
try {
await _auth.sendPasswordResetEmail(email: email);
Timer(
const Duration(seconds: 3),
() => CustomWidgets().moveToPage(
page: const Home(), context: context, replacement: true),
);
} catch (e) {
// error handling here
}
}
這將從 firebase 發送一個 email 到選定的 email 以重置密碼。
邏輯結束后,請撥打此電話 function。
bool _isEmailVerified = false;
Timer? timer;
final FirebaseAuth _auth = FirebaseAuth.instance;
初始化方法
@override
void initState() {
_isEmailVerified = _auth.currentUser!.emailVerified;
if (!_isEmailVerified) {
sendEmailVerificationForUser();
timer = Timer.periodic(const Duration(seconds: 5), (timer) {
emailVerificationStatus(context);
});
}
email 驗證校驗 function
emailVerificationStatus(BuildContext context) async {
try {
await _auth.currentUser!.reload();
setState(() {
_isEmailVerified = _auth.currentUser!.emailVerified;
});
} catch (e) {
// handle the error here
}
setState(() {
_isEmailVerified = _auth.currentUser!.emailVerified;
});
if (_isEmailVerified) {
timer?.cancel();
// and move to next page
}}
發送 email 驗證 function
Future sendEmailVerificationForUser() async {
try {
await FirebaseAuth.instance.currentUser!.sendEmailVerification();
} catch (e) {
// error handling}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.