Skip to content

Commit 2cfff7e

Browse files
LinusUThexXTURBOXxmatthewtsmith
authored
🎉 Add support for ephemeral sessions (#64)
Co-authored-by: Nico Mexis <nico.mexis@kabelmail.de> Co-authored-by: Matthew Smith <matthew.smith@apptreesoftware.com> Co-authored-by: Nico Mexis <nico.mexis@kabelmail.de> Co-authored-by: Matthew Smith <matthew.smith@apptreesoftware.com>
1 parent 9af584d commit 2cfff7e

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

android/src/main/kotlin/com/linusu/flutter_web_auth/FlutterWebAuthPlugin.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,17 @@ class FlutterWebAuthPlugin(private val context: Context): MethodCallHandler {
2828
"authenticate" -> {
2929
val url = Uri.parse(call.argument("url"))
3030
val callbackUrlScheme = call.argument<String>("callbackUrlScheme")!!
31+
val preferEphemeral = call.argument<Boolean>("preferEphemeral")!!
3132

3233
callbacks[callbackUrlScheme] = resultCallback
3334

3435
val intent = CustomTabsIntent.Builder().build()
3536
val keepAliveIntent = Intent(context, KeepAliveService::class.java)
3637

3738
intent.intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
39+
if (preferEphemeral) {
40+
intent.intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
41+
}
3842
intent.intent.putExtra("android.support.customtabs.extra.KEEP_ALIVE", keepAliveIntent)
3943

4044
intent.launchUrl(context, url)

ios/Classes/SwiftFlutterWebAuthPlugin.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class SwiftFlutterWebAuthPlugin: NSObject, FlutterPlugin {
1414
if call.method == "authenticate" {
1515
let url = URL(string: (call.arguments as! Dictionary<String, AnyObject>)["url"] as! String)!
1616
let callbackURLScheme = (call.arguments as! Dictionary<String, AnyObject>)["callbackUrlScheme"] as! String
17+
let preferEphemeral = (call.arguments as! Dictionary<String, AnyObject>)["preferEphemeral"] as! Bool
1718

1819
var sessionToKeepAlive: Any? = nil // if we do not keep the session alive, it will get closed immediately while showing the dialog
1920
let completionHandler = { (url: URL?, err: Error?) in
@@ -26,7 +27,7 @@ public class SwiftFlutterWebAuthPlugin: NSObject, FlutterPlugin {
2627
return
2728
}
2829
}
29-
30+
3031
if #available(iOS 11, *) {
3132
if case SFAuthenticationError.canceledLogin = err {
3233
result(FlutterError(code: "CANCELED", message: "User canceled login", details: nil))
@@ -50,6 +51,7 @@ public class SwiftFlutterWebAuthPlugin: NSObject, FlutterPlugin {
5051
return
5152
}
5253

54+
session.prefersEphemeralWebBrowserSession = preferEphemeral
5355
session.presentationContextProvider = provider
5456
}
5557

lib/flutter_web_auth.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ class FlutterWebAuth {
2828
/// The page pointed to by [url] will be loaded and displayed to the user. From the page, the user can authenticate herself and grant access to the app. On completion, the service will send a callback URL with an authentication token, and this URL will be result of the returned [Future].
2929
///
3030
/// [callbackUrlScheme] should be a string specifying the scheme of the url that the page will redirect to upon successful authentication.
31-
static Future<String> authenticate({required String url, required String callbackUrlScheme}) async {
31+
/// [preferEphemeral] if this is specified as `true`, an ephemeral web browser session will be used where possible (`FLAG_ACTIVITY_NO_HISTORY` on Android, `prefersEphemeralWebBrowserSession` on iOS/macOS)
32+
static Future<String> authenticate({required String url, required String callbackUrlScheme, bool? preferEphemeral}) async {
3233
WidgetsBinding.instance?.removeObserver(_resumedObserver); // safety measure so we never add this observer twice
3334
WidgetsBinding.instance?.addObserver(_resumedObserver);
3435
return await _channel.invokeMethod('authenticate', <String, dynamic>{
3536
'url': url,
3637
'callbackUrlScheme': callbackUrlScheme,
38+
'preferEphemeral': preferEphemeral ?? false,
3739
}) as String;
3840
}
3941

0 commit comments

Comments
 (0)