160 lines
4.8 KiB
Dart
160 lines
4.8 KiB
Dart
// import 'package:flutter/material.dart';
|
|
// import 'package:frontend/routes/custom_router.dart';
|
|
//
|
|
// class MyApp extends StatelessWidget {
|
|
// const MyApp({super.key});
|
|
//
|
|
// @override
|
|
// Widget build(BuildContext context) {
|
|
// return MaterialApp.router(
|
|
// title: 'TRIP MANAGEMENT',
|
|
// routerConfig: router,
|
|
// debugShowCheckedModeBanner: false,
|
|
// );
|
|
// }
|
|
// }
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/rendering.dart';
|
|
import 'package:flutter_quill/flutter_quill.dart';
|
|
import 'package:frontend/routes/custom_router.dart';
|
|
import 'package:flutter_localizations/flutter_localizations.dart';
|
|
import 'package:flutter_quill/flutter_quill.dart' hide Text;
|
|
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';
|
|
|
|
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;
|
|
|
|
// print(">>> _isAuthRedirect: $_isAuthRedirect");
|
|
print(">>> Auth code found at startup: $_authCode");
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
handleTokenUsingMS(_authCode);
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
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.router(
|
|
title: 'TRIP MANAGEMENT',
|
|
routerConfig: router,
|
|
debugShowCheckedModeBanner: false,
|
|
localizationsDelegates: const [
|
|
GlobalMaterialLocalizations.delegate,
|
|
GlobalWidgetsLocalizations.delegate,
|
|
GlobalCupertinoLocalizations.delegate,
|
|
FlutterQuillLocalizations.delegate, // ✅ Needed for flutter_quill
|
|
],
|
|
supportedLocales: const [
|
|
Locale('en'), // ✅ Add more if needed
|
|
],
|
|
);
|
|
}
|
|
}
|