feat(login): implement credential management for login modal

This commit is contained in:
zimplexing
2025-08-15 15:20:37 +08:00
parent 9b7833b430
commit e4ecd1339e
2 changed files with 54 additions and 2 deletions

View File

@@ -6,6 +6,7 @@ import useAuthStore from "@/stores/authStore";
import { useSettingsStore } from "@/stores/settingsStore";
import useHomeStore from "@/stores/homeStore";
import { api } from "@/services/api";
import { LoginCredentialsManager } from "@/services/storage";
import { ThemedView } from "./ThemedView";
import { ThemedText } from "./ThemedText";
import { StyledButton } from "./StyledButton";
@@ -22,6 +23,20 @@ const LoginModal = () => {
const pathname = usePathname();
const isSettingsPage = pathname.includes("settings");
// Load saved credentials when modal opens
useEffect(() => {
if (isLoginModalVisible && !isSettingsPage) {
const loadCredentials = async () => {
const savedCredentials = await LoginCredentialsManager.get();
if (savedCredentials) {
setUsername(savedCredentials.username);
setPassword(savedCredentials.password);
}
};
loadCredentials();
}
}, [isLoginModalVisible, isSettingsPage]);
// Focus management with better TV remote handling
useEffect(() => {
if (isLoginModalVisible && !isSettingsPage) {
@@ -51,10 +66,12 @@ const LoginModal = () => {
await api.login(isLocalStorage ? undefined : username, password);
await checkLoginStatus(apiBaseUrl);
await refreshPlayRecords();
// Save credentials on successful login
await LoginCredentialsManager.save({ username, password });
Toast.show({ type: "success", text1: "登录成功" });
hideLoginModal();
setUsername("");
setPassword("");
// Show disclaimer alert after successful login
Alert.alert(

View File

@@ -9,6 +9,7 @@ const STORAGE_KEYS = {
FAVORITES: "mytv_favorites",
PLAY_RECORDS: "mytv_play_records",
SEARCH_HISTORY: "mytv_search_history",
LOGIN_CREDENTIALS: "mytv_login_credentials",
} as const;
// --- Type Definitions (aligned with api.ts) ---
@@ -37,6 +38,11 @@ export interface AppSettings {
m3uUrl: string;
}
export interface LoginCredentials {
username: string;
password: string;
}
// --- Helper ---
const generateKey = (source: string, id: string) => `${source}+${id}`;
@@ -302,3 +308,32 @@ export class SettingsManager {
await AsyncStorage.removeItem(STORAGE_KEYS.SETTINGS);
}
}
// --- LoginCredentialsManager (Uses AsyncStorage) ---
export class LoginCredentialsManager {
static async get(): Promise<LoginCredentials | null> {
try {
const data = await AsyncStorage.getItem(STORAGE_KEYS.LOGIN_CREDENTIALS);
return data ? JSON.parse(data) : null;
} catch (error) {
console.info("Failed to get login credentials:", error);
return null;
}
}
static async save(credentials: LoginCredentials): Promise<void> {
try {
await AsyncStorage.setItem(STORAGE_KEYS.LOGIN_CREDENTIALS, JSON.stringify(credentials));
} catch (error) {
console.error("Failed to save login credentials:", error);
}
}
static async clear(): Promise<void> {
try {
await AsyncStorage.removeItem(STORAGE_KEYS.LOGIN_CREDENTIALS);
} catch (error) {
console.error("Failed to clear login credentials:", error);
}
}
}