简体   繁体   中英

send verification mail in flutter/dart development

I'm New to flutter and dart mobile app development. how to implement forgot password and send verification mail in flutter/dart development or is there any way to implement to send mail.

I don't think there is any way to send an email from your flutter application. This is something I would definitely implement on a backend server.

I would implement a 'forgot password' button in flutter, which triggers a http call to the backend which then triggers the password generation and email sending.

Read more about making calls over the internet here: [ https://flutter.dev/docs/cookbook/networking/fetch-data][1]

Edited: This links relates into how you can make requests and accept responses from the internet(to make requests in the server when the password is forgotten as in this case).

So you have to create a back-end service to accept the calls from your clients in this case your client is flutter app, I can't provide the way to do it here with code, you should learn more to do that. After you have created your server and implemented the logic to accept and validate requests, then the server can return a request with a link in it to reset the password, and you can render the response in the flutter app, so you can make another request with new credentials. The other way is to send the link(request) via email to the client(the email user provided when he/she registered) and the client then can change the password accordingly.

Hope this can help.

Yes there are some ways. The most common would be to use firebase as a backend server handling these requests.

You could do it like this

Add these packages to your flutter apps pubspec.yaml file

// latest version
firebase_core: ^1.17.0
firebase_auth: ^3.3.18 

On forgot password button click

once you've completed the logic necessary before making the request, call this 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
}

}

This will send an email from firebase to the selected email to reset password.

On email verification request

Once the logic is over, call this function.

bool _isEmailVerified = false;
Timer? timer;
final FirebaseAuth _auth = FirebaseAuth.instance;

the initstate method

@override
void initState() {
_isEmailVerified = _auth.currentUser!.emailVerified;

if (!_isEmailVerified) {
  sendEmailVerificationForUser();
  timer = Timer.periodic(const Duration(seconds: 5), (timer) {
    emailVerificationStatus(context);
  });
}

email verification check 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
  
}}

send email verification function

Future sendEmailVerificationForUser() async {
try {
  await FirebaseAuth.instance.currentUser!.sendEmailVerification();
} catch (e) {
  // error handling}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM