将用户重定向回Flutter应用以进行Supabase密码重置。

huangapple go评论62阅读模式
英文:

Redirect user back to flutter app for supabase password reset

问题

以下是翻译好的内容:

当用户在密码重置电子邮件中点击"重置电子邮件"链接时,他们会被重定向回我的Flutter应用程序,但不会进入SetNewPasswordScreen。如何确保用户被特定地重定向到SetNewPasswordScreen?

总之,我试图实现以下用户流程:

  1. 用户在ForgotPasswordScreen上提交他们的电子邮件地址。
  2. 用户收到电子邮件。
  3. 用户在电子邮件中点击"重置电子邮件"链接。
  4. 用户被重定向到SetNewPasswordScreen。

以下调用会触发密码重置电子邮件发送给用户:

final response = await supaClient.auth.resetPasswordForEmail(email,
    redirectTo: kIsWeb
        ? null
        : 'io.supabase.pickleballislife://forgotpassword-callback/');

在我的ios/Runner/Info.plist文件中,我插入了以下块:

<key>CFBundleURLTypes</key>
<array>
    <dict>
    <key>CFBundleTypeRole</key>
    <string>Editor</string>
    <key>CFBundleURLSchemes</key>
    <array>
        <string>'io.supabase.pickleballislife://forgotpassword-callback/'</string>
    </array>
    </dict>
</array>

在我的android/app/src/main/AndroidManifest.xml文件中,我插入了以下块:

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <!-- 接受以YOUR_SCHEME://YOUR_HOST开头的URI -->
    <data
    android:scheme="io.supabase.pickleballislife"
    android:host="forgotpassword-callback" />
</intent-filter>

SetNewPasswordScreen的路由为:

static const ROUTE_NAME = '/forgotpassword-callback';

我已经在我的路由中如下定义它:

SetNewPasswordScreen.ROUTE_NAME: (BuildContext context) {
    return SetNewPasswordScreen();
},

在我的ForgotPasswordScreen上,我已设置了以下AuthChangeEvent监听器:

@override
void initState() {
    super.initState();
    final _authSubscription = supaClient.auth.onAuthStateChange.listen((data) {
        final AuthChangeEvent event = data.event;
        if (event == AuthChangeEvent.passwordRecovery) {
            goToNamed(
                SetNewPasswordScreen.ROUTE_NAME,
                replace: true,
            );
        }
    });
    _authSubscription.cancel();
}

如何确保用户在电子邮件中点击"重置密码"链接后,他们被重定向到SetNewPassword屏幕,而不仅仅是我的移动应用程序?

英文:

I'm fairly new to supabase, flutter, and programming in general. I'm trying to achieve the following:

When the user taps the "Reset Email" link in the password reset email, they are redirected back to my Flutter app, but not to the SetNewPasswordScreen. How can I make sure that the user is redirected to the SetNewPasswordScreen specifically?

In summary, this is the user flow I am trying to achieve:

  1. User submits their email address on ForgotPasswordScreen
  2. User recieves email
  3. User taps "Reset Email" link in email
  4. User is redirected to SetNewPasswordScreen

The following call triggers the password reset email to be sent to the user:

      final response = await supaClient.auth.resetPasswordForEmail(email,
          redirectTo: kIsWeb
              ? null
              : &#39;io.supabase.pickleballislife://forgotpassword-callback/&#39;);

In my ios/Runner/Info.plist file, I've inserted the following block:

	&lt;key&gt;CFBundleURLTypes&lt;/key&gt;
	&lt;array&gt;
		&lt;dict&gt;
		&lt;key&gt;CFBundleTypeRole&lt;/key&gt;
		&lt;string&gt;Editor&lt;/string&gt;
		&lt;key&gt;CFBundleURLSchemes&lt;/key&gt;
		&lt;array&gt;
			&lt;string&gt;&#39;io.supabase.pickleballislife://forgotpassword-callback/&#39;&lt;/string&gt;
		&lt;/array&gt;
		&lt;/dict&gt;
	&lt;/array&gt;

And in my android/app/src/main/AndroidManifest.xml file, I've inserted the following block:

&lt;intent-filter&gt;
				&lt;action android:name=&quot;android.intent.action.VIEW&quot; /&gt;
				&lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
				&lt;category android:name=&quot;android.intent.category.BROWSABLE&quot; /&gt;
				&lt;!-- Accepts URIs that begin with YOUR_SCHEME://YOUR_HOST --&gt;
				&lt;data
				android:scheme=&quot;io.supabase.pickleballislife&quot;
				android:host=&quot;forgotpassword-callback&quot; /&gt;
			&lt;/intent-filter&gt;

The route for the SetNewPasswordScreen is:

  static const ROUTE_NAME = &#39;/forgotpassword-callback&#39;;

and I've defined it in my routes the following way:

SetNewPasswordScreen.ROUTE_NAME: (BuildContext context) {
    return SetNewPasswordScreen();
  },

And on my ForgotPasswordScreen, I've set up the following AuthChangeEvent listener:

  @override
  void initState() {
    super.initState();
    final _authSubscription = supaClient.auth.onAuthStateChange.listen((data) {
      final AuthChangeEvent event = data.event;
      if (event == AuthChangeEvent.passwordRecovery) {
        goToNamed(
          SetNewPasswordScreen.ROUTE_NAME,
          replace: true,
        );
      }
    });
    _authSubscription.cancel();
  }

How can I ensure that once the user taps the "Reset Password" link in their email, they are redirected to the SetNewPassword screen and not just my mobile app in general?

答案1

得分: 1

Your plist file should contain only io.supabase.pickleballislife, because that is your scheme

&lt;key&gt;FlutterDeepLinkingEnabled&lt;/key&gt;
&lt;true/&gt;
&lt;key&gt;CFBundleURLTypes&lt;/key&gt;
    &lt;array&gt;
        &lt;dict&gt;
        &lt;key&gt;CFBundleTypeRole&lt;/key&gt;
        &lt;string&gt;Editor&lt;/string&gt;
        &lt;key&gt;CFBundleURLSchemes&lt;/key&gt;
        &lt;array&gt;
            &lt;string&gt;io.supabase.pickleballislife&lt;/string&gt;
        &lt;/array&gt;
        &lt;/dict&gt;
    &lt;/array&gt;

Then instead of going to io.supabase.pickleballislife://forgotpassword-callback/, you can go to io.supabase.pickleballislife://callback/forgotpassword. The key is that we are now redirecting to the /forgotpassword path. BTW, there is nothing wrong with having forgotpassword-callback as the host, but it might be confusing, so I have changed it to callback. You may want to update the setting on Android as well.

Now you can setup your routes using GoRouter,

GoRouter(
  routes: [
    ...
    GoRoute(
      path: &#39;/forgotpassword&#39;,
      builder: (context, state) =&gt; SetNewPasswordScreen(),
    ),
  ],
);

You also need to setup some additional configuration on both iOS and Android side to enable Flutter deep linking. You can find the instruction here.

With this, you should be able to call the following and the user will be redirected to the password reset page! Also remember to update the URL configuration on your Supabase dashboard to accept io.supabase.pickleballislife://callback/forgotpassword/ as additional URL.

final response = await supaClient.auth.resetPasswordForEmail(email,
          redirectTo: kIsWeb
              ? null
              : &#39;io.supabase.pickleballislife://callback/forgotpassword/&#39;);
英文:

Your plist file should contain only io.supabase.pickleballislife, because that is your scheme

&lt;key&gt;FlutterDeepLinkingEnabled&lt;/key&gt;
&lt;true/&gt;
&lt;key&gt;CFBundleURLTypes&lt;/key&gt;
    &lt;array&gt;
        &lt;dict&gt;
        &lt;key&gt;CFBundleTypeRole&lt;/key&gt;
        &lt;string&gt;Editor&lt;/string&gt;
        &lt;key&gt;CFBundleURLSchemes&lt;/key&gt;
        &lt;array&gt;
            &lt;string&gt;io.supabase.pickleballislife&lt;/string&gt;
        &lt;/array&gt;
        &lt;/dict&gt;
    &lt;/array&gt;

Then instead of going to io.supabase.pickleballislife://forgotpassword-callback/, you can go to io.supabase.pickleballislife://callback/forgotpassword. The key is that we are now redirecting to the /forgotpassword path. BTW, there is nothing wrong with having forgotpassword-callback as the host, but it might be confusing, so I have changed it to callback. You may want to update the setting on Android as well.

Now you can setup your routes using GoRouter,

GoRouter(
  routes: [
    ...
    GoRoute(
      path: &#39;/forgotpassword&#39;,
      builder: (context, state) =&gt; SetNewPasswordScreen(),
    ),
  ],
);

You also need to setup some additional configuration on both iOS and Android side to enable Flutter deep linking. You can find the instruction here.

With this, you should be able to call the following and the user will be redirected to the password reset page! Also remember to update the URL configuration on your Supabase dashboard to accept io.supabase.pickleballislife://callback/forgotpassword/ as additional URL.

final response = await supaClient.auth.resetPasswordForEmail(email,
          redirectTo: kIsWeb
              ? null
              : &#39;io.supabase.pickleballislife://callback/forgotpassword/&#39;);

huangapple
  • 本文由 发表于 2023年4月7日 05:19:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953871.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定