diff --git a/flutter_web_auth_2/example/lib/main.dart b/flutter_web_auth_2/example/lib/main.dart index 2c3e6b6..0423cc1 100644 --- a/flutter_web_auth_2/example/lib/main.dart +++ b/flutter_web_auth_2/example/lib/main.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'dart:io' show HttpServer, Platform; +import 'package:desktop_webview_window/desktop_webview_window.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -63,7 +64,12 @@ const html = ''' '''; -void main() => runApp(const MyApp()); +void main(List args) { + if (runWebViewTitleBarWidget(args)) { + return; + } + runApp(const MyApp()); +} class MyApp extends StatefulWidget { const MyApp({Key? key}) : super(key: key); @@ -95,7 +101,7 @@ class MyAppState extends State { // Windows needs some callback URL on localhost req.response.write( - (Platform.isWindows || Platform.isLinux) + (Platform.isLinux) ? html.replaceFirst( 'CALLBACK_URL_HERE', 'http://localhost:43824/success?code=1337', @@ -122,9 +128,7 @@ class MyAppState extends State { // Windows needs some callback URL on localhost final callbackUrlScheme = - !kIsWeb && (Platform.isWindows || Platform.isLinux) - ? 'http://localhost:43824' - : 'foobar'; + !kIsWeb && (Platform.isLinux) ? 'http://localhost:43824' : 'foobar'; try { final result = await FlutterWebAuth2.authenticate( @@ -157,7 +161,9 @@ class MyAppState extends State { Text('Status: $_status\n'), const SizedBox(height: 80), ElevatedButton( - onPressed: authenticate, + onPressed: () async { + await authenticate(); + }, child: const Text('Authenticate'), ), ], diff --git a/flutter_web_auth_2/example/pubspec.yaml b/flutter_web_auth_2/example/pubspec.yaml index 283ecb6..2587630 100644 --- a/flutter_web_auth_2/example/pubspec.yaml +++ b/flutter_web_auth_2/example/pubspec.yaml @@ -8,6 +8,7 @@ environment: dependencies: cupertino_icons: ^1.0.3 + desktop_webview_window: ^0.2.3 flutter: sdk: flutter flutter_web_auth_2: ^3.0.0 diff --git a/flutter_web_auth_2/lib/flutter_web_auth_2.dart b/flutter_web_auth_2/lib/flutter_web_auth_2.dart index 1867d7d..c9084b9 100644 --- a/flutter_web_auth_2/lib/flutter_web_auth_2.dart +++ b/flutter_web_auth_2/lib/flutter_web_auth_2.dart @@ -10,6 +10,7 @@ export 'src/options.dart'; export 'src/unsupported.dart' if (dart.library.io) 'src/server.dart' if (dart.library.html) 'src/web.dart'; +export 'src/windows.dart'; class _OnAppLifecycleResumeObserver extends WidgetsBindingObserver { final Function onResumed; diff --git a/flutter_web_auth_2/lib/src/windows.dart b/flutter_web_auth_2/lib/src/windows.dart new file mode 100644 index 0000000..0030ad8 --- /dev/null +++ b/flutter_web_auth_2/lib/src/windows.dart @@ -0,0 +1,76 @@ +import 'dart:async'; + +import 'package:desktop_webview_window/desktop_webview_window.dart'; +import 'package:flutter/cupertino.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_web_auth_2_platform_interface/flutter_web_auth_2_platform_interface.dart'; +import 'package:path_provider/path_provider.dart'; + +class FlutterWebAuth2WindowsPlugin extends FlutterWebAuth2Platform { + bool authenticated = false; + Webview? webview; + + static void registerWith() { + FlutterWebAuth2Platform.instance = FlutterWebAuth2WindowsPlugin(); + } + + @override + Future authenticate({ + required String url, + required String callbackUrlScheme, + required Map options, + }) async { + if (!await WebviewWindow.isWebviewAvailable()) { + //Microsofts WebView2 must be installed for this to work + throw StateError('Webview is not available'); + } + //Reset + authenticated = false; + webview?.close(); + + final c = Completer(); + debugPrint( + '''Launching webview with url: $url, callbackUrlScheme: $callbackUrlScheme, tmpDir: ${(await getTemporaryDirectory()).path}''', + ); + webview = await WebviewWindow.create( + configuration: CreateConfiguration( + windowHeight: 720, + windowWidth: 1280, + title: 'Authenticate', + titleBarTopPadding: 0, + userDataFolderWindows: (await getTemporaryDirectory()).path, + ), + ); + webview!.addOnUrlRequestCallback((url) { + final uri = Uri.parse(url); + if (uri.scheme == callbackUrlScheme) { + authenticated = true; + webview?.close(); + /** + * Not setting the webview to null will cause a crash if the + * application tries to open another webview + */ + webview = null; + c.complete(url); + } + }); + unawaited( + webview!.onClose.whenComplete( + () { + /** + * Not setting the webview to null will cause a crash if the + * application tries to open another webview + */ + webview = null; + if (!authenticated) { + c.completeError( + PlatformException(code: 'CANCELED', message: 'User canceled'), + ); + } + }, + ), + ); + webview!.launch(url); + return c.future; + } +} diff --git a/flutter_web_auth_2/pubspec.yaml b/flutter_web_auth_2/pubspec.yaml index 1e0bd44..658423e 100644 --- a/flutter_web_auth_2/pubspec.yaml +++ b/flutter_web_auth_2/pubspec.yaml @@ -24,11 +24,13 @@ environment: flutter: ">=3.0.0" dependencies: + desktop_webview_window: ^0.2.3 flutter: sdk: flutter flutter_web_auth_2_platform_interface: ^3.0.0 flutter_web_plugins: sdk: flutter + path_provider: ^2.1.2 url_launcher: ^6.1.6 window_to_front: ^0.0.3 @@ -54,5 +56,5 @@ flutter: pluginClass: FlutterWebAuth2WebPlugin fileName: src/web.dart windows: - dartPluginClass: FlutterWebAuth2ServerPlugin - fileName: src/server.dart + dartPluginClass: FlutterWebAuth2WindowsPlugin + fileName: src/windows.dart