Skip to content

Commit

Permalink
Merge remote-tracking branch 'Mino5531/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
ThexXTURBOXx committed Jan 22, 2024
2 parents 92f942e + d5c2de0 commit 25cfa23
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 8 deletions.
18 changes: 12 additions & 6 deletions flutter_web_auth_2/example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -63,7 +64,12 @@ const html = '''
</html>
''';

void main() => runApp(const MyApp());
void main(List<String> args) {
if (runWebViewTitleBarWidget(args)) {
return;
}
runApp(const MyApp());
}

class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
Expand Down Expand Up @@ -95,7 +101,7 @@ class MyAppState extends State<MyApp> {

// 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',
Expand All @@ -122,9 +128,7 @@ class MyAppState extends State<MyApp> {

// 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(
Expand Down Expand Up @@ -157,7 +161,9 @@ class MyAppState extends State<MyApp> {
Text('Status: $_status\n'),
const SizedBox(height: 80),
ElevatedButton(
onPressed: authenticate,
onPressed: () async {
await authenticate();
},
child: const Text('Authenticate'),
),
],
Expand Down
1 change: 1 addition & 0 deletions flutter_web_auth_2/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions flutter_web_auth_2/lib/flutter_web_auth_2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
76 changes: 76 additions & 0 deletions flutter_web_auth_2/lib/src/windows.dart
Original file line number Diff line number Diff line change
@@ -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<String> authenticate({
required String url,
required String callbackUrlScheme,
required Map<String, dynamic> 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<String>();
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;
}
}
6 changes: 4 additions & 2 deletions flutter_web_auth_2/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -54,5 +56,5 @@ flutter:
pluginClass: FlutterWebAuth2WebPlugin
fileName: src/web.dart
windows:
dartPluginClass: FlutterWebAuth2ServerPlugin
fileName: src/server.dart
dartPluginClass: FlutterWebAuth2WindowsPlugin
fileName: src/windows.dart

0 comments on commit 25cfa23

Please sign in to comment.