This commit is contained in:
2025-06-22 17:24:43 +02:00
parent 7a1f76ee85
commit e30635304b
7 changed files with 40 additions and 47 deletions

View File

@ -2,7 +2,6 @@ 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/models/user.dart';
@ -10,6 +9,8 @@ class AuthController extends GetxController {
final EncryptedSharedPreferencesAsync storage =
EncryptedSharedPreferencesAsync.getInstance();
final GetConnect http = GetConnect();
RxnString token = RxnString();
Rxn<User> user = Rxn<User>();
RxBool isLoading = false.obs;
@ -38,7 +39,8 @@ class AuthController extends GetxController {
if (token.value != null) {
try {
await http.post(
Uri.parse('https://api.f0ck.me/logout'),
'https://api.f0ck.me/logout',
{},
headers: {
'Authorization': 'Bearer ${token.value}',
'Content-Type': 'application/json',
@ -55,13 +57,15 @@ class AuthController extends GetxController {
isLoading.value = true;
error.value = null;
try {
final http.Response response = await http.post(
Uri.parse('https://api.f0ck.me/login'),
final Response<dynamic> response = await http.post(
'https://api.f0ck.me/login',
json.encode({'username': username, 'password': password}),
headers: {'Content-Type': 'application/json'},
body: json.encode({'username': username, 'password': password}),
);
if (response.statusCode == 200) {
final dynamic data = json.decode(response.body);
final dynamic data = response.body is String
? json.decode(response.body)
: response.body;
if (data['token'] != null) {
await saveToken(data['token']);
user.value = User.fromJson(data);
@ -83,12 +87,14 @@ class AuthController extends GetxController {
Future<void> fetchUserInfo() async {
if (token.value == null) return;
try {
final http.Response response = await http.get(
Uri.parse('https://api.f0ck.me/login/check'),
final Response<dynamic> response = await http.get(
'https://api.f0ck.me/login/check',
headers: {'Authorization': 'Bearer ${token.value}'},
);
if (response.statusCode == 200) {
final dynamic data = json.decode(response.body);
final dynamic data = response.body is String
? json.decode(response.body)
: response.body;
user.value = User.fromJson(data);
} else {
await logout();