From 445103859ba9da4a201d8c168eb0b1efc76597b9 Mon Sep 17 00:00:00 2001 From: Newman Chow Date: Thu, 22 Feb 2024 19:18:20 +0800 Subject: [PATCH] Close change password page on success #32 --- .../com/authgear/flutter/AuthgearPlugin.kt | 11 +++++++ .../com/authgear/flutter/WebViewActivity.kt | 3 ++ example/lib/main.dart | 30 +++++++++++++++++++ ios/Classes/SwiftAuthgearPlugin.swift | 12 +++----- lib/src/container.dart | 9 +++++- lib/src/type.dart | 3 ++ 6 files changed, 59 insertions(+), 9 deletions(-) diff --git a/android/src/main/kotlin/com/authgear/flutter/AuthgearPlugin.kt b/android/src/main/kotlin/com/authgear/flutter/AuthgearPlugin.kt index 5b5ff7b..2b016e2 100644 --- a/android/src/main/kotlin/com/authgear/flutter/AuthgearPlugin.kt +++ b/android/src/main/kotlin/com/authgear/flutter/AuthgearPlugin.kt @@ -45,6 +45,7 @@ class AuthgearPlugin: FlutterPlugin, ActivityAware, MethodCallHandler, PluginReg companion object { private const val TAG_AUTHENTICATION = 1 private const val TAG_OPEN_URL = 2 + private const val SDK_REDIRECT_URI = "authgearsdk://host/path" private val wechat: HashMap = hashMapOf() @@ -58,6 +59,16 @@ class AuthgearPlugin: FlutterPlugin, ActivityAware, MethodCallHandler, PluginReg return true } + internal fun onSDKRedirectURI(uri: Uri): Boolean { + val uriWithoutQuery = uri.buildUpon().clearQuery().fragment("").build().toString() + if (uriWithoutQuery != SDK_REDIRECT_URI) { + return false + } + + throw CancelException(); + return true + } + fun wechatErrorResult(errCode: Int, errStr: String, result: Result) { if (errCode == -2) { result.cancel() diff --git a/android/src/main/kotlin/com/authgear/flutter/WebViewActivity.kt b/android/src/main/kotlin/com/authgear/flutter/WebViewActivity.kt index 6ecfff3..1d6276d 100644 --- a/android/src/main/kotlin/com/authgear/flutter/WebViewActivity.kt +++ b/android/src/main/kotlin/com/authgear/flutter/WebViewActivity.kt @@ -47,6 +47,9 @@ class WebViewActivity: AppCompatActivity() { if (url != null && AuthgearPlugin.onWechatRedirectURI(url)) { return true } + if (url != null && AuthgearPlugin.onSDKRedirectURI(url)) { + return true + } return super.shouldOverrideUrlLoading(view, request) } diff --git a/example/lib/main.dart b/example/lib/main.dart index 82e8f10..8fb996b 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -515,6 +515,16 @@ class _MyAppState extends State { _onPressOpenSettings(context); }, ), + SessionStateButton( + sessionState: _authgear.sessionState, + targetState: SessionState.authenticated, + label: "Change Password", + onPressed: _unconfigured || _loading + ? null + : () { + _onPressChangePassword(context); + }, + ), SessionStateButton( sessionState: _authgear.sessionState, targetState: SessionState.authenticated, @@ -808,6 +818,26 @@ class _MyAppState extends State { } } + Future _onPressChangePassword(BuildContext context) async { + try { + setState(() { + _loading = true; + }); + await _authgear.open( + page: SettingsPage.changePassword, + colorScheme: _getColorScheme(context), + wechatRedirectURI: wechatRedirectURI, + closeOnSuccess: true + ); + } catch (e) { + onError(context, e); + } finally { + setState(() { + _loading = false; + }); + } + } + Future _onPressShowAuthTime(BuildContext context) async { showDialog( context: context, diff --git a/ios/Classes/SwiftAuthgearPlugin.swift b/ios/Classes/SwiftAuthgearPlugin.swift index ebd3e08..b4b8bcc 100644 --- a/ios/Classes/SwiftAuthgearPlugin.swift +++ b/ios/Classes/SwiftAuthgearPlugin.swift @@ -191,7 +191,7 @@ public class SwiftAuthgearPlugin: NSObject, FlutterPlugin, ASWebAuthenticationPr return } - result(FlutterError.unreachable) + result(FlutterError.cancel) return } @@ -237,7 +237,7 @@ public class SwiftAuthgearPlugin: NSObject, FlutterPlugin, ASWebAuthenticationPr return } - result(FlutterError.unreachable) + result(FlutterError.cancel) return } controller.navigationBarBackgroundColor = navigationBarBackgroundColor @@ -263,13 +263,13 @@ public class SwiftAuthgearPlugin: NSObject, FlutterPlugin, ASWebAuthenticationPr return } - result(FlutterError.unreachable) + result(FlutterError.cancel) return } if #available(iOS 12, *) { let session = ASWebAuthenticationSession( url: url, - callbackURLScheme: "nocallback", + callbackURLScheme: "authgearsdk", completionHandler: completionHandler ) if #available(iOS 13, *) { @@ -895,10 +895,6 @@ fileprivate extension NSError { } fileprivate extension FlutterError { - static var unreachable: FlutterError { - return FlutterError(code: "UNREACHABLE", message: "unreachable", details: nil) - } - static var cancel: FlutterError { return FlutterError(code: "CANCEL", message: "cancel", details: nil) } diff --git a/lib/src/container.dart b/lib/src/container.dart index b6eb1cf..285d282 100644 --- a/lib/src/container.dart +++ b/lib/src/container.dart @@ -1,3 +1,4 @@ +import 'dart:ffi'; import 'dart:math' show Random; import 'dart:async' show StreamController; import 'dart:convert' show jsonEncode, utf8; @@ -418,6 +419,7 @@ class Authgear implements AuthgearHttpClientDelegate { List? uiLocales, ColorScheme? colorScheme, String? wechatRedirectURI, + bool? closeOnSuccess, }) async { final oidcConfig = await _apiClient.fetchOIDCConfiguration(); final endpoint = Uri.parse(oidcConfig.authorizationEndpoint); @@ -431,6 +433,9 @@ class Authgear implements AuthgearHttpClientDelegate { if (colorScheme != null) { q["x_color_scheme"] = colorScheme.name; } + if (closeOnSuccess == true) { + q["redirect_uri"] = "authgearsdk://host/path"; + } final url = origin.replace(path: path, queryParameters: q).toString(); return openURL(url: url, wechatRedirectURI: wechatRedirectURI); @@ -441,12 +446,14 @@ class Authgear implements AuthgearHttpClientDelegate { List? uiLocales, ColorScheme? colorScheme, String? wechatRedirectURI, + bool? closeOnSuccess, }) async { return _openAuthgearURL( path: page.path, uiLocales: uiLocales, colorScheme: colorScheme, - wechatRedirectURI: wechatRedirectURI); + wechatRedirectURI: wechatRedirectURI, + closeOnSuccess: closeOnSuccess); } Future refreshIDToken() async { diff --git a/lib/src/type.dart b/lib/src/type.dart index a44f2e6..6188d32 100644 --- a/lib/src/type.dart +++ b/lib/src/type.dart @@ -42,6 +42,7 @@ enum AuthenticationPage { enum SettingsPage { settings, + changePassword, identity, } @@ -55,6 +56,8 @@ extension SettingsPageExtension on SettingsPage { switch (this) { case SettingsPage.settings: return "/settings"; + case SettingsPage.changePassword: + return "/settings/change_password"; case SettingsPage.identity: return "/settings/identity"; }