You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter集成Supabase:密码重置链接无法自动跳转至改密页面

解决Flutter+Supabase密码重置深度链接不跳转问题

核心问题分析

你当前的代码仅声明了_setupDeepLinkHandling方法但未实现实际逻辑——Supabase不会自动解析深度链接中的重置令牌,必须手动调用resetPasswordSession方法处理链接,才能触发AuthChangeEvent.passwordRecovery事件。另外Windows平台的深度链接需要单独配置,不能只依赖Android的配置规则。

具体修复步骤

1. 补全深度链接处理逻辑

在_MyAppState中实现链接监听和处理方法,将获取到的深度链接传递给Supabase解析:

void _setupDeepLinkHandling() async {
  _appLinks = AppLinks();
  // 处理APP冷启动时的深度链接
  final initialUri = await _appLinks.getInitialAppLink();
  if (initialUri != null) {
    _handleResetPasswordLink(initialUri);
  }
  // 处理APP运行时的深度链接
  _linkSubscription = _appLinks.uriLinkStream.listen((uri) {
    _handleResetPasswordLink(uri);
  });
}

void _handleResetPasswordLink(Uri uri) {
  // 校验是否为目标密码重置链接
  if (uri.scheme == 'com.example.inventory' && uri.host == 'reset-password') {
    // 让Supabase解析链接中的令牌,触发passwordRecovery事件
    Supabase.instance.client.auth.resetPasswordSession(uri.toString());
  }
}

2. 优化Auth状态监听的导航逻辑

添加WidgetsBinding.instance.addPostFrameCallback确保导航上下文已初始化,避免currentState为空导致导航失败:

void _setupAuthListener() {
  _authSubscription = Supabase.instance.client.auth.onAuthStateChange.listen((data) {
    final event = data.event;
    if (event == AuthChangeEvent.passwordRecovery) {
      WidgetsBinding.instance.addPostFrameCallback((_) {
        _navigatorKey.currentState?.pushReplacement(
          MaterialPageRoute(builder: (context) => const ChangePasswordScreen()),
        );
      });
    }
  });
}

3. 配置Windows平台深度链接

在pubspec.yaml中添加Windows协议处理配置:

flutter:
  windows:
    protocol_handlers:
      com.example.inventory:
        name: InventoryApp
        schemes:
          - com.example.inventory

或者手动修改windows/runner/Runner.rc文件,添加注册表协议注册(二选一即可):

REGEDIT4

[HKEY_CLASSES_ROOT\com.example.inventory]
@="URL:com.example.inventory Protocol"
"URL Protocol"=""

[HKEY_CLASSES_ROOT\com.example.inventory\shell]

[HKEY_CLASSES_ROOT\com.example.inventory\shell\open]

[HKEY_CLASSES_ROOT\com.example.inventory\shell\open\command]
@="\"%1\" \"%*\""

4. 配置Supabase控制台允许重定向URL

登录Supabase控制台,进入项目的Auth -> URL Configuration,将com.example.inventory://reset-password添加到Redirect URLs列表中,否则Supabase会拦截该重定向请求。

验证流程

  1. 重新运行Windows应用
  2. 请求密码重置并点击邮件中的链接
  3. 应用打开后会自动触发passwordRecovery事件,跳转至修改密码页面

内容的提问来源于stack exchange,提问作者digital doctor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 11:53:18