Skip to content

Commit

Permalink
Close change password page on success authgear#32
Browse files Browse the repository at this point in the history
  • Loading branch information
IniZio committed Feb 22, 2024
1 parent a4861ae commit 4451038
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 9 deletions.
11 changes: 11 additions & 0 deletions android/src/main/kotlin/com/authgear/flutter/AuthgearPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, MethodChannel> = hashMapOf()

Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
30 changes: 30 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,16 @@ class _MyAppState extends State<MyApp> {
_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,
Expand Down Expand Up @@ -808,6 +818,26 @@ class _MyAppState extends State<MyApp> {
}
}

Future<void> _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<void> _onPressShowAuthTime(BuildContext context) async {
showDialog(
context: context,
Expand Down
12 changes: 4 additions & 8 deletions ios/Classes/SwiftAuthgearPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public class SwiftAuthgearPlugin: NSObject, FlutterPlugin, ASWebAuthenticationPr
return
}

result(FlutterError.unreachable)
result(FlutterError.cancel)
return
}

Expand Down Expand Up @@ -237,7 +237,7 @@ public class SwiftAuthgearPlugin: NSObject, FlutterPlugin, ASWebAuthenticationPr
return
}

result(FlutterError.unreachable)
result(FlutterError.cancel)
return
}
controller.navigationBarBackgroundColor = navigationBarBackgroundColor
Expand All @@ -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, *) {
Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 8 additions & 1 deletion lib/src/container.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:ffi';
import 'dart:math' show Random;
import 'dart:async' show StreamController;
import 'dart:convert' show jsonEncode, utf8;
Expand Down Expand Up @@ -418,6 +419,7 @@ class Authgear implements AuthgearHttpClientDelegate {
List<String>? uiLocales,
ColorScheme? colorScheme,
String? wechatRedirectURI,
bool? closeOnSuccess,
}) async {
final oidcConfig = await _apiClient.fetchOIDCConfiguration();
final endpoint = Uri.parse(oidcConfig.authorizationEndpoint);
Expand All @@ -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);
Expand All @@ -441,12 +446,14 @@ class Authgear implements AuthgearHttpClientDelegate {
List<String>? uiLocales,
ColorScheme? colorScheme,
String? wechatRedirectURI,
bool? closeOnSuccess,
}) async {
return _openAuthgearURL(
path: page.path,
uiLocales: uiLocales,
colorScheme: colorScheme,
wechatRedirectURI: wechatRedirectURI);
wechatRedirectURI: wechatRedirectURI,
closeOnSuccess: closeOnSuccess);
}

Future<void> refreshIDToken() async {
Expand Down
3 changes: 3 additions & 0 deletions lib/src/type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ enum AuthenticationPage {

enum SettingsPage {
settings,
changePassword,
identity,
}

Expand All @@ -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";
}
Expand Down

0 comments on commit 4451038

Please sign in to comment.