45 lines
1.6 KiB
Dart
45 lines
1.6 KiB
Dart
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
class AuthManager {
|
|
// 定義鍵值常數,避免硬編碼字串出錯
|
|
static const String _tokenKey = 'auth_token';
|
|
static const String _userIdKey = 'user_id'; // [新增] 用於儲存工號的鍵值
|
|
|
|
/// 儲存登入資訊:包含 Token 與工號
|
|
/// 當登入成功時,同時呼叫此方法
|
|
static Future<void> saveLoginInfo(String token, String userId) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString(_tokenKey, token);
|
|
await prefs.setString(_userIdKey, userId);
|
|
print('Login info saved: UserID=$userId');
|
|
}
|
|
|
|
/// 讀取儲存的工號 (userid)
|
|
/// 如果沒找到則回傳 null,可用於判斷是否需重新登入
|
|
static Future<String?> getUserId() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
return prefs.getString(_userIdKey);
|
|
}
|
|
|
|
/// 讀取儲存的 Token
|
|
static Future<String?> getToken() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
return prefs.getString(_tokenKey);
|
|
}
|
|
|
|
/// 移除所有登入資訊 (用於登出)
|
|
/// 確保 Token 與工號同步清除,保障資安
|
|
static Future<void> logout() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.remove(_tokenKey);
|
|
await prefs.remove(_userIdKey);
|
|
print('User logged out, auth info cleared.');
|
|
}
|
|
|
|
/// 檢查是否已登入 (Token 與 UserID 皆存在)
|
|
static Future<bool> isLoggedIn() async {
|
|
final token = await getToken();
|
|
final userId = await getUserId();
|
|
return token != null && userId != null;
|
|
}
|
|
} |