This commit is contained in:
98
lib/controller/auth_controller.dart
Normal file
98
lib/controller/auth_controller.dart
Normal file
@ -0,0 +1,98 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:get/get.dart';
|
||||
import 'package:encrypt_shared_preferences/provider.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import 'package:f0ckapp/controller/media_controller.dart';
|
||||
|
||||
class AuthController extends GetxController {
|
||||
final EncryptedSharedPreferencesAsync storage =
|
||||
EncryptedSharedPreferencesAsync.getInstance();
|
||||
final MediaController mediaController = Get.find<MediaController>();
|
||||
|
||||
RxnString token = RxnString();
|
||||
RxnInt userId = RxnInt();
|
||||
RxnString avatarUrl = RxnString();
|
||||
RxnString username = RxnString();
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
loadToken();
|
||||
}
|
||||
|
||||
Future<void> loadToken() async {
|
||||
token.value = await storage.getString('token');
|
||||
if (token.value != null) {
|
||||
await fetchUserInfo();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> saveToken(String newToken) async {
|
||||
token.value = newToken;
|
||||
await storage.setString('token', newToken);
|
||||
await fetchUserInfo();
|
||||
}
|
||||
|
||||
Future<void> logout() async {
|
||||
if (token.value != null) {
|
||||
try {
|
||||
await http.post(
|
||||
Uri.parse('https://api.f0ck.me/logout'),
|
||||
headers: {
|
||||
'Authorization': 'Bearer ${token.value}',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
);
|
||||
await mediaController.loadMediaItems();
|
||||
mediaController.mediaItems.refresh();
|
||||
} catch (e) {
|
||||
//
|
||||
}
|
||||
}
|
||||
token.value = null;
|
||||
userId.value = null;
|
||||
avatarUrl.value = null;
|
||||
username.value = null;
|
||||
await storage.remove('token');
|
||||
}
|
||||
|
||||
Future<bool> login(String username, String password) async {
|
||||
final http.Response response = await http.post(
|
||||
Uri.parse('https://api.f0ck.me/login'),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: json.encode({'username': username, 'password': password}),
|
||||
);
|
||||
if (response.statusCode == 200) {
|
||||
final dynamic data = json.decode(response.body);
|
||||
if (data['token'] != null) {
|
||||
await saveToken(data['token']);
|
||||
await mediaController.loadMediaItems();
|
||||
mediaController.mediaItems.refresh();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Future<void> fetchUserInfo() async {
|
||||
if (token.value == null) return;
|
||||
final http.Response response = await http.get(
|
||||
Uri.parse('https://api.f0ck.me/login/check'),
|
||||
headers: {'Authorization': 'Bearer ${token.value}'},
|
||||
);
|
||||
if (response.statusCode == 200) {
|
||||
final dynamic data = json.decode(response.body);
|
||||
userId.value = data['userid'] != null
|
||||
? int.parse(data['userid'].toString())
|
||||
: null;
|
||||
avatarUrl.value = data['avatar'] != null
|
||||
? 'https://f0ck.me/t/${data['avatar']}.webp'
|
||||
: null;
|
||||
username.value = data['user'];
|
||||
} else {
|
||||
await logout();
|
||||
}
|
||||
}
|
||||
}
|
@ -15,9 +15,13 @@ class MyTranslations extends Translations {
|
||||
static Future<void> loadTranslations() async {
|
||||
final locales = ['en_US', 'de_DE', 'fr_FR', 'nl_NL'];
|
||||
for (final locale in locales) {
|
||||
final String jsonString = await rootBundle.loadString('assets/i18n/$locale.json');
|
||||
final String jsonString = await rootBundle.loadString(
|
||||
'assets/i18n/$locale.json',
|
||||
);
|
||||
final Map<String, dynamic> jsonMap = json.decode(jsonString);
|
||||
_translations[locale] = jsonMap.map((key, value) => MapEntry(key, value.toString()));
|
||||
_translations[locale] = jsonMap.map(
|
||||
(key, value) => MapEntry(key, value.toString()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -23,6 +23,8 @@ class MediaController extends GetxController {
|
||||
late RxBool drawerSwipeEnabled = true.obs;
|
||||
final Rx<PageTransition> transitionType = PageTransition.opacity.obs;
|
||||
|
||||
MediaItem? selectedItem;
|
||||
|
||||
@override
|
||||
void onInit() async {
|
||||
super.onInit();
|
||||
|
Reference in New Issue
Block a user