You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mohemm-flutter-app/lib/widgets/sso_webview_widget.dart

119 lines
3.6 KiB
Dart

import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/classes/utils.dart';
import 'package:mohem_flutter_app/widgets/app_bar_widget.dart';
import 'package:webview_flutter/webview_flutter.dart';
// Enum for SSO types
enum SsoType { forTicket, forEnsat, forCourses }
class SsoLoginWebView extends StatefulWidget {
final String? url;
final String? jwtToken;
final SsoType ssoType;
const SsoLoginWebView({Key? key, required this.url, this.jwtToken, required this.ssoType}) : super(key: key);
@override
State<SsoLoginWebView> createState() => _SsoLoginWebViewState();
}
class _SsoLoginWebViewState extends State<SsoLoginWebView> {
late final WebViewController _controller;
String? url;
bool _isTicketLoadingVisible = false;
void _showTicketLoading() {
if (widget.ssoType != SsoType.forTicket || _isTicketLoadingVisible || !mounted) return;
_isTicketLoadingVisible = true;
Utils.showLoading(context);
}
void _hideTicketLoading() {
if (widget.ssoType != SsoType.forTicket || !_isTicketLoadingVisible || !mounted) return;
_isTicketLoadingVisible = false;
Utils.hideLoading(context);
}
@override
void initState() {
super.initState();
url = widget.url ?? "";
_controller =
WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setNavigationDelegate(
NavigationDelegate(
onProgress: (int progress) {
print("WebView is loading (progress: $progress%)");
if (widget.ssoType == SsoType.forEnsat && progress == 100) {
Utils.hideLoading(context);
}
},
onPageStarted: (String url) {
print("Page started loading: $url");
_showTicketLoading();
},
onPageFinished: (String url) {
print("Page finished loading: $url");
_hideTicketLoading();
},
onHttpError: (HttpResponseError error) {
print("HTTP error: ${error.toString()} for URL: ${error.response!.statusCode}");
_hideTicketLoading();
},
onWebResourceError: (WebResourceError error) {
print("Web resource error: ${error.description} for URL: ${error.errorType}");
_hideTicketLoading();
},
),
);
// Load content based on SSO type
if (widget.ssoType == SsoType.forEnsat) {
// For ENSAT, just load the URL directly (GET request)
_controller.loadRequest(Uri.parse(url!));
} else {
if (widget.ssoType == SsoType.forTicket) {
WidgetsBinding.instance.addPostFrameCallback((_) {
_showTicketLoading();
});
}
// For Paxes (Ticket, Courses), use HTML form POST
_controller.loadHtmlString(_buildHtmlContent());
}
}
// Build HTML content for the SSO form
String _buildHtmlContent() {
String formAction;
if (widget.ssoType == SsoType.forTicket) {
formAction = "https://www.paxes.com/sso/hmg";
} else {
formAction = url ?? "";
}
return '''
<!DOCTYPE html>
<html>
<body onload="document.forms[0].submit()">
<form method="POST" action="$formAction">
<input type="hidden" name="JWTToken" value="${widget.jwtToken}" />
</form>
<h1></h1>
</body>
</html>''';
}
@override
void dispose() {
_hideTicketLoading();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(appBar: AppBarWidget(context, title: "MoheM", showWorkListSettingButton: false), body: SafeArea(child: WebViewWidget(controller: _controller)));
}
}