Merge branch 'main' of bitbucket.org:venbainformationtechnology/ts-tat
This commit is contained in:
commit
7b515573c7
@ -1,7 +1,6 @@
|
||||
import 'dart:convert';
|
||||
// import 'dart:ui' as html;
|
||||
import 'dart:html' as html;
|
||||
|
||||
// import 'dart:ui' as html;
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:fluttertoast/fluttertoast.dart';
|
||||
@ -714,7 +713,9 @@ class _LoginWidgetState extends State<LoginWidget> {
|
||||
children: [
|
||||
Expanded(
|
||||
child: ElevatedButton(
|
||||
onPressed: () {},
|
||||
onPressed: () {
|
||||
handleMS();
|
||||
},
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.white, // Button color
|
||||
foregroundColor: Colors.black, // Text color
|
||||
@ -820,4 +821,56 @@ class _LoginWidgetState extends State<LoginWidget> {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Future<void> handleMS() async {
|
||||
|
||||
final url = '$apiUrl/auth/mslogin';
|
||||
print(url);
|
||||
try {
|
||||
final response = await http.get(
|
||||
Uri.parse(url),
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
);
|
||||
print("inside try method");
|
||||
if (response.statusCode == 200) {
|
||||
final authUrl = json.decode(response.body)['auth_url'];
|
||||
print("authurl - $authUrl");
|
||||
if(authUrl != ''){
|
||||
// final prefs = await SharedPreferences.getInstance();
|
||||
// await prefs.setString('auth_token', authUrl);
|
||||
print('i have auth URL');
|
||||
// canLaunchUrl(authUrl);
|
||||
|
||||
if (kIsWeb) {
|
||||
print("kIsWeb");
|
||||
// Use web redirect (e.g., via JS interop or window.location.href)
|
||||
// redirectTo(url);
|
||||
|
||||
html.window.location.href = authUrl;
|
||||
} else {
|
||||
// For mobile/desktop, open in external browser
|
||||
// launchUrl(Uri.parse(url),
|
||||
// mode: LaunchMode.externalApplication);
|
||||
}
|
||||
// final result = await FlutterWebAuth.authenticate(
|
||||
// url: authUrl,
|
||||
// callbackUrlScheme: "myapp", // Use a custom scheme you registered
|
||||
// );
|
||||
}else{
|
||||
print('auth URL not Founded');
|
||||
throw Exception('auth URL not Founded');
|
||||
}
|
||||
}else{
|
||||
final errorMessage = json.decode(response.body)['message'];
|
||||
print(errorMessage);
|
||||
throw Exception(errorMessage);
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
print("Error: $e");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
76
lib/app.dart
76
lib/app.dart
@ -22,6 +22,11 @@ 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: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});
|
||||
@ -32,6 +37,7 @@ class MyApp extends StatefulWidget {
|
||||
|
||||
class _MyAppState extends State<MyApp> {
|
||||
String? _authCode;
|
||||
String? userRole;
|
||||
bool _isAuthRedirect = false;
|
||||
@override
|
||||
void initState() {
|
||||
@ -51,10 +57,80 @@ class _MyAppState extends State<MyApp> {
|
||||
// - call an API
|
||||
// - store this in a Provider
|
||||
// - or just pass it through GoRouter as usual
|
||||
|
||||
// if(_authCode){
|
||||
handleTokenUsingMS(_authCode);
|
||||
// }else{
|
||||
// _isAuthRedirect = false;
|
||||
// }
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> handleTokenUsingMS(_authCode) async {
|
||||
|
||||
final url = '$apiUrl/auth/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);
|
||||
}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) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user