import 'dart:convert'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import '../../../../core/errors/exceptions.dart'; import '../models/user_model.dart'; abstract class AuthLocalDataSource { Future cacheUser(UserModel user); Future getCachedUser(); Future clearCache(); Future cacheToken(String token); Future getCachedToken(); } class AuthLocalDataSourceImpl implements AuthLocalDataSource { final FlutterSecureStorage secureStorage; static const String userKey = 'CACHED_USER'; static const String tokenKey = 'AUTH_TOKEN'; AuthLocalDataSourceImpl({required this.secureStorage}); @override Future cacheUser(UserModel user) async { try { final userJson = json.encode(user.toJson()); await secureStorage.write(key: userKey, value: userJson); } catch (e) { throw CacheException('Failed to cache user'); } } @override Future getCachedUser() async { try { final userJson = await secureStorage.read(key: userKey); if (userJson != null) { final userMap = json.decode(userJson) as Map; return UserModel.fromJson(userMap); } return null; } catch (e) { throw CacheException('Failed to get cached user'); } } @override Future clearCache() async { try { await secureStorage.delete(key: userKey); await secureStorage.delete(key: tokenKey); } catch (e) { throw CacheException('Failed to clear cache'); } } @override Future cacheToken(String token) async { try { await secureStorage.write(key: tokenKey, value: token); } catch (e) { throw CacheException('Failed to cache token'); } } @override Future getCachedToken() async { try { return await secureStorage.read(key: tokenKey); } catch (e) { throw CacheException('Failed to get cached token'); } } }