Added profile screen with API interaction, better .env usage
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:http/http.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'dart:convert';
|
||||
|
||||
Future<bool> checkToken() async
|
||||
{
|
||||
@@ -9,10 +10,26 @@ Future<bool> checkToken() async
|
||||
return date.isAfter(DateTime.now());
|
||||
}
|
||||
|
||||
// Future<Map<String, dynamic>> fetchProfileData(String login) async
|
||||
// {
|
||||
//
|
||||
// }
|
||||
Future<Map<String, dynamic>> fetchProfileData(String login) async
|
||||
{
|
||||
final uri = Uri.https(
|
||||
'api.intra.42.fr',
|
||||
'/v2/users/$login',
|
||||
);
|
||||
final token = await getToken() ?? '';
|
||||
final response = await http.get(
|
||||
uri,
|
||||
headers: {
|
||||
'Authorization': 'Bearer $token',
|
||||
'Accept': 'application/json',
|
||||
},
|
||||
);
|
||||
if (response.statusCode == 200) {
|
||||
return jsonDecode(response.body) as Map<String, dynamic>;
|
||||
}
|
||||
else { throw Exception('Failed to fetch user: ${response.statusCode}'); }
|
||||
}
|
||||
|
||||
Future<void> saveToken(String token, String refresh, DateTime expiration) async
|
||||
{
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
|
||||
Reference in New Issue
Block a user