颤动Firebase-点击Firebase发送的电子邮件验证链接时如何打开应用程序? [英] Flutter Firebase - How to open the app when tapping on email verification link send by Firebase?

查看:15
本文介绍了颤动Firebase-点击Firebase发送的电子邮件验证链接时如何打开应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过我的Ffltter应用程序上的Firebase发送电子邮件验证链接。但我想修改链接,以便当用户点击它时,它将把他们带到我定义的应用程序屏幕。所以基本上是深度链接。我环顾四周,找到了深度链接的资源,但它们都是在处理定制URL时考虑到了新闻稿之类的东西。

但我希望修改Firebase在应用程序中打开特定屏幕的深层链接中发送的验证电子邮件。

推荐答案

在用户注册帐户后,您需要路由到一个新类,其中 您将发送sendEmailVerification();

init函数

参见以下代码

user = auth.currentUser!; 
user.sendEmailVerification();

在initState()中,还要设置一个计时器,用于检查电子邮件是否定期验证。

所以,假设每隔五秒调用此函数,它将检查电子邮件是否经过验证。

引用以下函数

Future<void> checkEmailVerified() async {
    user = auth.currentUser!;
    await user.reload();
    if (user.emailVerified) {
      timer.cancel();
      Navigator.of(context).pushNamed('/home');
    }
  }

如果用户电子邮件通过验证,则将用户转到主屏幕

并同时处置计时器

参考下面给出的完整代码

import 'dart:async';

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:service_provider/constant/constant.dart';
import 'package:sizer/sizer.dart';

class VerifyScreen extends StatefulWidget {
  @override
  _VerifyScreenState createState() => _VerifyScreenState();
}

class _VerifyScreenState extends State<VerifyScreen> {
  final auth = FirebaseAuth.instance;
  late User user;
  late Timer timer;
  @override
  void initState() {
    user = auth.currentUser!;
    user.sendEmailVerification();
    timer = Timer.periodic(Duration(seconds: 5), (timer) {
      checkEmailVerified();
    });
    super.initState();
  }

  @override
  void dispose() {
    timer.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Padding(
              padding: const EdgeInsets.all(0.0),
              child: Image.asset('assets/images/gif.gif',
                  fit: BoxFit.contain, height: 18.h),
            ),
            Padding(
              padding: const EdgeInsets.all(25.0),
              child: RichText(
                text: TextSpan(
                  text: 'An email has been sent to ',
                  style: GoogleFonts.spartan(
                    color: Colors.black,
                    // textStyle: Theme.of(context).textTheme.headline4,
                    fontSize: 14,
                    fontWeight: FontWeight.w400,
                  ),
                  children: <TextSpan>[
                    TextSpan(
                      text: '"${user.email}"',
                      style: GoogleFonts.spartan(
                        color: Color(Const.primary),
                        // textStyle: Theme.of(context).textTheme.headline4,
                        fontSize: 14,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                    const TextSpan(text: ' please verify'),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Future<void> checkEmailVerified() async {
    user = auth.currentUser!;
    await user.reload();
    if (user.emailVerified) {
      timer.cancel();
      Navigator.of(context).pushNamed('/home');
    }
  }
}

注意:

某些用户只需从此处关闭应用程序即可跳过此过程。所以,你可以做的是,当用户再次登录时,检查用户是否经过验证,如果用户的电子邮件没有再次验证,请转到验证页面

谢谢

这篇关于颤动Firebase-点击Firebase发送的电子邮件验证链接时如何打开应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆