Ulak/lib/services/auth_service.dart
2026-04-03 16:02:27 +03:00

66 lines
1.8 KiB
Dart

import 'package:get/get_instance/get_instance.dart';
import 'package:get/route_manager.dart';
import 'package:get/state_manager.dart';
import 'package:version_01/models/account_model.dart';
import 'package:version_01/routes/app_pages.dart';
import 'package:version_01/services/api_services.dart';
import 'package:version_01/services/storage_service.dart';
class AuthService extends GetxService {
late final StorageService _storageService;
late final ApiServices _apiServices;
final Rx<AccountModel?> currentAccount = Rx<AccountModel?>(null);
Future<AuthService> init() async {
_storageService = Get.find<StorageService>();
_apiServices = Get.find<ApiServices>();
_storageService.init();
return this;
}
Future<void> signIn() async {
final res = await _apiServices.post(
'/api/v1/login',
data: {'email': 'batuhancoskun@yaani.com', 'password': 'Batuhan1428'},
);
await _storageService.setValue(
StorageKeys.userToken,
res.data['Data']['token'].toString(),
);
}
Future<bool> isAuthenticated() async {
try {
final token = await _apiServices.get('/api/v1/login');
if (token.data) {
return true;
} else {
return false;
}
} catch (e) {
await _storageService.remove(StorageKeys.userToken);
return false;
}
}
Future<void> signOut() async {
await _storageService.remove(StorageKeys.userToken);
Get.offAllNamed(AppRoutes.SPLASH);
}
Future<AccountModel?> getAccount() async {
try {
final response = await _apiServices.get('/api/v1/account');
if (response.statusCode == 200) {
currentAccount.value = AccountModel.fromJson(response.data);
return AccountModel.fromJson(response.data);
} else {
return null;
}
} catch (e) {
return null;
}
}
}