Skip to content

Commit

Permalink
Merge pull request #78 from humhub/63-oauthexternal-auths-broken
Browse files Browse the repository at this point in the history
Fix external Auth
  • Loading branch information
luke- committed Jun 22, 2023
2 parents ca3b529 + 71c70b1 commit d8638ca
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
35 changes: 35 additions & 0 deletions lib/components/in_app_browser.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'dart:async';
import 'dart:developer';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:humhub/models/manifest.dart';
import 'package:humhub/util/extensions.dart';

class MyInAppBrowser extends InAppBrowser {
final Manifest manifest;
late InAppBrowserClassOptions options;
final Function concludeAuth;

MyInAppBrowser({required this.manifest, required this.concludeAuth}) {
options = InAppBrowserClassOptions(
crossPlatform: InAppBrowserOptions(hideUrlBar: false, toolbarTopBackgroundColor: HexColor(manifest.themeColor)),
inAppWebViewGroupOptions: InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(javaScriptEnabled: true, useShouldOverrideUrlLoading: true),
),
);
}

@override
Future<NavigationActionPolicy?>? shouldOverrideUrlLoading(NavigationAction navigationAction) async {
log("Browser closed!");

if (navigationAction.request.url!.origin.startsWith(manifest.baseUrl)) {
concludeAuth(navigationAction.request);
return NavigationActionPolicy.CANCEL;
}
return NavigationActionPolicy.ALLOW;
}

launchUrl(URLRequest urlRequest) {
openUrlRequest(urlRequest: urlRequest, options: options);
}
}
30 changes: 27 additions & 3 deletions lib/pages/web_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import 'package:permission_handler/permission_handler.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:humhub/util/router.dart' as m;

import '../components/in_app_browser.dart';
import '../models/hum_hub.dart';

class WebViewApp extends ConsumerStatefulWidget {
Expand All @@ -31,6 +32,7 @@ class WebViewApp extends ConsumerStatefulWidget {

class WebViewAppState extends ConsumerState<WebViewApp> {
late InAppWebViewController webViewController;
late MyInAppBrowser authBrowser;
late Manifest manifest;
late URLRequest _initialRequest;
final _options = InAppWebViewGroupOptions(
Expand All @@ -41,13 +43,29 @@ class WebViewAppState extends ConsumerState<WebViewApp> {
javaScriptEnabled: true,
),
);

PullToRefreshController? _pullToRefreshController;
late PullToRefreshOptions _pullToRefreshOptions;

@override
void initState() {
super.initState();
}

Future<NavigationActionPolicy> shouldOverride(NavigationAction navigationAction) async {
return NavigationActionPolicy.ALLOW;
}

@override
Widget build(BuildContext context) {
_initialRequest = getInitRequest(context);
_initialRequest = _initRequest;
_pullToRefreshController = initPullToRefreshController;
authBrowser = MyInAppBrowser(
manifest: manifest,
concludeAuth: (URLRequest request) {
_concludeAuth(request);
},
);
return WillPopScope(
onWillPop: () => webViewController.exitApp(context, ref),
child: Scaffold(
Expand Down Expand Up @@ -90,7 +108,8 @@ class WebViewAppState extends ConsumerState<WebViewApp> {
// 1st check if url is not def. app url and open it in a browser or inApp.
final url = action.request.url!.origin;
if (!url.startsWith(manifest.baseUrl)) {
launchUrl(action.request.url!, mode: LaunchMode.externalApplication);
authBrowser.launchUrl(action.request);
/*launchUrl(action.request.url!, mode: LaunchMode.inAppWebView);*/
return NavigationActionPolicy.CANCEL;
}
// 2nd Append customHeader if url is in app redirect and CANCEL the requests without custom headers
Expand All @@ -102,6 +121,11 @@ class WebViewAppState extends ConsumerState<WebViewApp> {
return NavigationActionPolicy.ALLOW;
}

_concludeAuth(URLRequest request) {
authBrowser.close();
webViewController.loadUrl(urlRequest: request);
}

_onWebViewCreated(InAppWebViewController controller) async {
await controller.addWebMessageListener(
WebMessageListener(
Expand Down Expand Up @@ -154,7 +178,7 @@ class WebViewAppState extends ConsumerState<WebViewApp> {
return request;
}

URLRequest getInitRequest(BuildContext context) {
URLRequest get _initRequest {
//Append random hash to customHeaders in this state the header should always exist.
bool isHideDialog = ref.read(humHubProvider).isHideDialog;
Map<String, String> customHeaders = {};
Expand Down

0 comments on commit d8638ca

Please sign in to comment.