mirror of
https://github.com/zimplexing/OrionTV.git
synced 2026-02-24 12:24:42 +08:00
feat(login): implement credential management for login modal
This commit is contained in:
@@ -6,6 +6,7 @@ import useAuthStore from "@/stores/authStore";
|
|||||||
import { useSettingsStore } from "@/stores/settingsStore";
|
import { useSettingsStore } from "@/stores/settingsStore";
|
||||||
import useHomeStore from "@/stores/homeStore";
|
import useHomeStore from "@/stores/homeStore";
|
||||||
import { api } from "@/services/api";
|
import { api } from "@/services/api";
|
||||||
|
import { LoginCredentialsManager } from "@/services/storage";
|
||||||
import { ThemedView } from "./ThemedView";
|
import { ThemedView } from "./ThemedView";
|
||||||
import { ThemedText } from "./ThemedText";
|
import { ThemedText } from "./ThemedText";
|
||||||
import { StyledButton } from "./StyledButton";
|
import { StyledButton } from "./StyledButton";
|
||||||
@@ -22,6 +23,20 @@ const LoginModal = () => {
|
|||||||
const pathname = usePathname();
|
const pathname = usePathname();
|
||||||
const isSettingsPage = pathname.includes("settings");
|
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
|
// Focus management with better TV remote handling
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isLoginModalVisible && !isSettingsPage) {
|
if (isLoginModalVisible && !isSettingsPage) {
|
||||||
@@ -51,10 +66,12 @@ const LoginModal = () => {
|
|||||||
await api.login(isLocalStorage ? undefined : username, password);
|
await api.login(isLocalStorage ? undefined : username, password);
|
||||||
await checkLoginStatus(apiBaseUrl);
|
await checkLoginStatus(apiBaseUrl);
|
||||||
await refreshPlayRecords();
|
await refreshPlayRecords();
|
||||||
|
|
||||||
|
// Save credentials on successful login
|
||||||
|
await LoginCredentialsManager.save({ username, password });
|
||||||
|
|
||||||
Toast.show({ type: "success", text1: "登录成功" });
|
Toast.show({ type: "success", text1: "登录成功" });
|
||||||
hideLoginModal();
|
hideLoginModal();
|
||||||
setUsername("");
|
|
||||||
setPassword("");
|
|
||||||
|
|
||||||
// Show disclaimer alert after successful login
|
// Show disclaimer alert after successful login
|
||||||
Alert.alert(
|
Alert.alert(
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ const STORAGE_KEYS = {
|
|||||||
FAVORITES: "mytv_favorites",
|
FAVORITES: "mytv_favorites",
|
||||||
PLAY_RECORDS: "mytv_play_records",
|
PLAY_RECORDS: "mytv_play_records",
|
||||||
SEARCH_HISTORY: "mytv_search_history",
|
SEARCH_HISTORY: "mytv_search_history",
|
||||||
|
LOGIN_CREDENTIALS: "mytv_login_credentials",
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
// --- Type Definitions (aligned with api.ts) ---
|
// --- Type Definitions (aligned with api.ts) ---
|
||||||
@@ -37,6 +38,11 @@ export interface AppSettings {
|
|||||||
m3uUrl: string;
|
m3uUrl: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface LoginCredentials {
|
||||||
|
username: string;
|
||||||
|
password: string;
|
||||||
|
}
|
||||||
|
|
||||||
// --- Helper ---
|
// --- Helper ---
|
||||||
const generateKey = (source: string, id: string) => `${source}+${id}`;
|
const generateKey = (source: string, id: string) => `${source}+${id}`;
|
||||||
|
|
||||||
@@ -302,3 +308,32 @@ export class SettingsManager {
|
|||||||
await AsyncStorage.removeItem(STORAGE_KEYS.SETTINGS);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user