322 lines
10 KiB
Dart
322 lines
10 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
// import 'package:flutter/rendering.dart';
|
|
import 'dart:html' as html;
|
|
import 'package:frontend/config/apiUrl.dart'; // 1 newly added
|
|
import 'package:frontend/services/apiService.dart';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_localizations/flutter_localizations.dart';
|
|
import 'package:flutter_quill/flutter_quill.dart';
|
|
import 'package:frontend/routes/custom_router.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'Screens/myTemplates/templateTest.dart';
|
|
|
|
class MyApp extends StatefulWidget {
|
|
const MyApp({super.key});
|
|
|
|
@override
|
|
State<MyApp> createState() => _MyAppState();
|
|
}
|
|
|
|
class _MyAppState extends State<MyApp> {
|
|
final ApiService apiService = ApiService();
|
|
String? _authCode;
|
|
String? userRole;
|
|
bool _isAuthRedirect = false;
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
// SemanticsBinding.instance.ensureSemantics(); // ✅ Safe here
|
|
if (kIsWeb) {
|
|
final uri = Uri.parse(html.window.location.href);
|
|
if (uri.path == '/authredirection' &&
|
|
uri.queryParameters['code'] != null) {
|
|
_authCode = uri.queryParameters['code'];
|
|
// _isAuthRedirect = true;
|
|
html.window.console.log("Auth code detected: $_authCode");
|
|
// print(">>> _isAuthRedirect: $_isAuthRedirect");
|
|
print(">>> Auth code found at startup: $_authCode");
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
handleTokenUsingMS(_authCode);
|
|
});
|
|
} else {
|
|
html.window.console.log("No auth code found or wrong path.");
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> handleTokenUsingMS(authCode) async {
|
|
if (authCode == null) return;
|
|
|
|
final url = '$apiUrl/auth/verifyMSAuthUser?code=$authCode';
|
|
try {
|
|
final response = await http.get(
|
|
Uri.parse(url),
|
|
headers: {'Content-Type': 'application/json'},
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
final MS_Token = json.decode(response.body)['token'];
|
|
print("MS_Token - $MS_Token");
|
|
|
|
if (MS_Token != '') {
|
|
print('Microsoft - Token Available');
|
|
await storeUserDetails(MS_Token);
|
|
|
|
print("userRole - $userRole");
|
|
|
|
if (userRole == "Travel Agent") {
|
|
router.go('/listTravelAgentPlan');
|
|
} else if (userRole == "Org Admin" || userRole == "Travel Admin") {
|
|
router.go('/listAllPlan');
|
|
} else {
|
|
router.go('/listPlan');
|
|
}
|
|
} else {
|
|
print('Microsoft - Token Not Available');
|
|
throw Exception('Token not Founded');
|
|
}
|
|
} else {
|
|
final errorMessage = json.decode(response.body)['message'];
|
|
print(errorMessage);
|
|
throw Exception(errorMessage);
|
|
}
|
|
} catch (e) {
|
|
print("Error: $e");
|
|
}
|
|
}
|
|
|
|
Future<void> storeUserDetails(String token) async {
|
|
try {
|
|
final parts = token.split('.');
|
|
if (parts.length != 3) throw Exception('Invalid token format');
|
|
|
|
final payload = json.decode(
|
|
utf8.decode(base64Url.decode(base64Url.normalize(parts[1]))),
|
|
);
|
|
|
|
final userData = payload['data'];
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString('auth_token', token);
|
|
await prefs.setString(
|
|
'user_data',
|
|
jsonEncode(userData),
|
|
); // Store full user data
|
|
|
|
if (userData != null) {
|
|
final pref = await SharedPreferences.getInstance();
|
|
await pref.setString('auth_token', token);
|
|
await pref.setString('user_data', jsonEncode(userData));
|
|
|
|
userRole = userData['role'];
|
|
|
|
print("userData - $userData");
|
|
print("userData11 - ${userData['role']}");
|
|
print("userData12 - $userRole");
|
|
}
|
|
apiService.getOrganizationData();
|
|
} catch (e) {
|
|
print('Error decoding token: $e');
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp.router(
|
|
title: 'TRIP MANAGEMENT',
|
|
debugShowCheckedModeBanner: false,
|
|
theme: ThemeData(
|
|
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
|
),
|
|
routerConfig: router, // <- your configured GoRouter or other RouterConfig
|
|
localizationsDelegates: const [
|
|
GlobalMaterialLocalizations.delegate,
|
|
GlobalCupertinoLocalizations.delegate,
|
|
GlobalWidgetsLocalizations.delegate,
|
|
FlutterQuillLocalizations.delegate,
|
|
],
|
|
supportedLocales: const [Locale('en'), Locale('es')],
|
|
);
|
|
}
|
|
}
|
|
|
|
//
|
|
// import 'package:flutter/foundation.dart';
|
|
// import 'package:flutter/material.dart';
|
|
// import 'package:flutter/rendering.dart';
|
|
// import 'package:flutter_quill/flutter_quill.dart';
|
|
// import 'package:flutter_localizations/flutter_localizations.dart';
|
|
//
|
|
// import 'package:frontend/routes/custom_router.dart';
|
|
// import 'dart:html' as html;
|
|
// import 'package:frontend/config/apiUrl.dart'; // 1 newly added
|
|
// import 'package:go_router/go_router.dart';
|
|
// import 'package:http/http.dart' as http; // 2 newly added
|
|
// import 'dart:convert'; // 3 newly added
|
|
//
|
|
// import 'package:shared_preferences/shared_preferences.dart';
|
|
//
|
|
// import 'Screens/myTemplates/templateTest.dart';
|
|
//
|
|
// class MyApp extends StatefulWidget {
|
|
// const MyApp({super.key});
|
|
//
|
|
// @override
|
|
// State<MyApp> createState() => _MyAppState();
|
|
// }
|
|
//
|
|
// class _MyAppState extends State<MyApp> {
|
|
// String? _authCode;
|
|
// String? userRole;
|
|
// bool _isAuthRedirect = false;
|
|
// @override
|
|
// void initState() {
|
|
// super.initState();
|
|
// SemanticsBinding.instance.ensureSemantics(); // ✅ Safe here
|
|
// if (kIsWeb) {
|
|
// final uri = Uri.parse(html.window.location.href);
|
|
// if (uri.path == '/authredirection' &&
|
|
// uri.queryParameters['code'] != null) {
|
|
// _authCode = uri.queryParameters['code'];
|
|
// // _isAuthRedirect = true;
|
|
// html.window.console.log("Auth code detected: $_authCode");
|
|
// // print(">>> _isAuthRedirect: $_isAuthRedirect");
|
|
// print(">>> Auth code found at startup: $_authCode");
|
|
//
|
|
// WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
// handleTokenUsingMS(_authCode);
|
|
// });
|
|
// } else {
|
|
// html.window.console.log("No auth code found or wrong path.");
|
|
// }
|
|
// }
|
|
// }
|
|
//
|
|
// Future<void> handleTokenUsingMS(authCode) async {
|
|
// if (authCode == null) return;
|
|
//
|
|
// final url = '$apiUrl/auth/verifyMSAuthUser?code=$authCode';
|
|
// try {
|
|
// final response = await http.get(
|
|
// Uri.parse(url),
|
|
// headers: {'Content-Type': 'application/json'},
|
|
// );
|
|
//
|
|
// if (response.statusCode == 200) {
|
|
// final MS_Token = json.decode(response.body)['token'];
|
|
// print("MS_Token - $MS_Token");
|
|
//
|
|
// if (MS_Token != '') {
|
|
// print('Microsoft - Token Available');
|
|
// await storeUserDetails(MS_Token);
|
|
//
|
|
// print("userRole - $userRole");
|
|
//
|
|
// if (userRole == "Travel Agent") {
|
|
// router.go('/listTravelAgentPlan');
|
|
// } else if (userRole == "Org Admin" || userRole == "Travel Admin") {
|
|
// router.go('/listAllPlan');
|
|
// } else {
|
|
// router.go('/listPlan');
|
|
// }
|
|
// } else {
|
|
// print('Microsoft - Token Not Available');
|
|
// throw Exception('Token not Founded');
|
|
// }
|
|
// } else {
|
|
// final errorMessage = json.decode(response.body)['message'];
|
|
// print(errorMessage);
|
|
// throw Exception(errorMessage);
|
|
// }
|
|
// } catch (e) {
|
|
// print("Error: $e");
|
|
// }
|
|
// }
|
|
//
|
|
// Future<void> storeUserDetails(String token) async {
|
|
// try {
|
|
// final parts = token.split('.');
|
|
// if (parts.length != 3) throw Exception('Invalid token format');
|
|
//
|
|
// final payload = json.decode(
|
|
// utf8.decode(base64Url.decode(base64Url.normalize(parts[1]))),
|
|
// );
|
|
//
|
|
// final userData = payload['data'];
|
|
//
|
|
// final prefs = await SharedPreferences.getInstance();
|
|
// await prefs.setString('auth_token', token);
|
|
// await prefs.setString(
|
|
// 'user_data',
|
|
// jsonEncode(userData),
|
|
// ); // Store full user data
|
|
//
|
|
// if (userData != null) {
|
|
// final pref = await SharedPreferences.getInstance();
|
|
// await pref.setString('auth_token', token);
|
|
// await pref.setString('user_data', jsonEncode(userData));
|
|
//
|
|
// userRole = userData['role'];
|
|
//
|
|
// print("userData - $userData");
|
|
// print("userData11 - ${userData['role']}");
|
|
// print("userData12 - $userRole");
|
|
// }
|
|
// } catch (e) {
|
|
// print('Error decoding token: $e');
|
|
// }
|
|
// }
|
|
//
|
|
// @override
|
|
// Widget build(BuildContext context) {
|
|
// // if (_isAuthRedirect) {
|
|
// // print("Rendering MicrosoftPage with code: $_authCode");
|
|
// // return MaterialApp(
|
|
// // home: MicrosoftPage(code: _authCode),
|
|
// // debugShowCheckedModeBanner: false,
|
|
// // );
|
|
// // }
|
|
// // return MaterialApp(
|
|
// // title: 'Quill Web Test',
|
|
// // localizationsDelegates: const [
|
|
// // GlobalMaterialLocalizations.delegate,
|
|
// // GlobalCupertinoLocalizations.delegate,
|
|
// // GlobalWidgetsLocalizations.delegate,
|
|
// // FlutterQuillLocalizations.delegate,
|
|
// // ],
|
|
// // supportedLocales: const [Locale('en')],
|
|
// // home: const MyHomePage(title: 'Flutter Demo Home Page'),
|
|
// // );
|
|
// return MaterialApp.router(
|
|
// title: 'TRIP MANAGEMENT',
|
|
// routerConfig: router,
|
|
// debugShowCheckedModeBanner: false,
|
|
// localizationsDelegates: const [
|
|
// GlobalMaterialLocalizations.delegate,
|
|
// GlobalCupertinoLocalizations.delegate,
|
|
// GlobalWidgetsLocalizations.delegate,
|
|
// FlutterQuillLocalizations.delegate,
|
|
// ],
|
|
// supportedLocales: const [
|
|
// Locale('en'),
|
|
// Locale('es'), // Add more as needed
|
|
// ],
|
|
// localeResolutionCallback: (locale, supportedLocales) {
|
|
// for (var supportedLocale in supportedLocales) {
|
|
// if (supportedLocale.languageCode == locale?.languageCode) {
|
|
// return supportedLocale;
|
|
// }
|
|
// }
|
|
// return supportedLocales.first;
|
|
// },
|
|
// );
|
|
// }
|
|
// }
|