Neda/Front/lib/services/auth_service.dart
2026-03-05 23:08:50 +03:30

34 lines
977 B
Dart

import 'package:shared_preferences/shared_preferences.dart';
class AuthService {
static const _tokenKey = 'auth_token';
static const _userIdKey = 'user_id';
Future<void> saveAuth({required String token, String? userId}) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(_tokenKey, token);
if (userId != null) await prefs.setString(_userIdKey, userId);
}
Future<String?> getToken() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString(_tokenKey);
}
Future<String?> getUserId() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString(_userIdKey);
}
Future<bool> isLoggedIn() async {
final token = await getToken();
return token != null && token.isNotEmpty;
}
Future<void> logout() async {
final prefs = await SharedPreferences.getInstance();
await prefs.remove(_tokenKey);
await prefs.remove(_userIdKey);
}
}