token from microsoft account
This commit is contained in:
parent
3fb412edba
commit
e4acf06234
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