update new version
This commit is contained in:
+20
-1
@@ -1,17 +1,36 @@
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class AuthManager {
|
||||
// --- 單例模式設定 ---
|
||||
static final AuthManager _instance = AuthManager._internal();
|
||||
factory AuthManager() => _instance;
|
||||
AuthManager._internal();
|
||||
|
||||
// 記憶體中的快取,讓您可以直接同步存取
|
||||
String? currentUserId;
|
||||
String? currentToken;
|
||||
|
||||
// 定義鍵值常數,避免硬編碼字串出錯
|
||||
static const String _tokenKey = 'auth_token';
|
||||
static const String _userIdKey = 'user_id'; // [新增] 用於儲存工號的鍵值
|
||||
|
||||
/// 初始化:App 啟動時呼叫一次,把硬碟資料讀進記憶體
|
||||
Future<void> initialize() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
currentUserId = prefs.getString(_userIdKey);
|
||||
currentToken = prefs.getString(_tokenKey);
|
||||
}
|
||||
|
||||
/// 儲存登入資訊:包含 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');
|
||||
|
||||
// 同步更新記憶體快取
|
||||
AuthManager().currentUserId = userId;
|
||||
AuthManager().currentToken = token;
|
||||
}
|
||||
|
||||
/// 讀取儲存的工號 (userid)
|
||||
|
||||
Reference in New Issue
Block a user