90 lines
2.6 KiB
Dart
90 lines
2.6 KiB
Dart
import 'dart:convert';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import '../../../core/constants/api_constants.dart';
|
|
|
|
class SocialAuthService {
|
|
Future<String?> _getEmail(SharedPreferences prefs) async {
|
|
String? email = prefs.getString('user_email');
|
|
if (email == null) {
|
|
final userJson = prefs.getString('user_details');
|
|
if (userJson != null) {
|
|
final user = jsonDecode(userJson);
|
|
email = user['email'];
|
|
}
|
|
}
|
|
return email;
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getStatus() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final email = await _getEmail(prefs);
|
|
final token = prefs.getString('token');
|
|
|
|
if (email == null) throw Exception("Session expired. Please log in again.");
|
|
|
|
final uri = Uri.parse('${ApiConstants.liveBaseUrl}/social/auth/status').replace(
|
|
queryParameters: {'userId': email},
|
|
);
|
|
|
|
final response = await http.get(
|
|
uri,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
if (token != null) 'Authorization': 'Bearer $token',
|
|
},
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
return jsonDecode(response.body);
|
|
} else {
|
|
throw Exception("Server error (${response.statusCode})");
|
|
}
|
|
}
|
|
|
|
Future<bool> disconnect() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final email = await _getEmail(prefs);
|
|
final token = prefs.getString('token');
|
|
|
|
if (email == null) return false;
|
|
|
|
final response = await http.post(
|
|
Uri.parse('${ApiConstants.liveBaseUrl}/social/auth/disconnect'),
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
if (token != null) 'Authorization': 'Bearer $token',
|
|
},
|
|
body: jsonEncode({'userId': email}),
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
final result = jsonDecode(response.body);
|
|
return result['ok'] == true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
Future<bool> saveAuth(String authToken) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final email = await _getEmail(prefs);
|
|
final token = prefs.getString('token');
|
|
if (email == null) return false;
|
|
|
|
final response = await http.post(
|
|
Uri.parse('${ApiConstants.liveBaseUrl}/social/auth/save-oauth'),
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
if (token != null) 'Authorization': 'Bearer $token',
|
|
},
|
|
body: jsonEncode({'auth': authToken, 'userId': email}),
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
final result = jsonDecode(response.body);
|
|
return result['ok'] == true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|