diff --git a/services/api.ts b/services/api.ts index d1b90b5..621a264 100644 --- a/services/api.ts +++ b/services/api.ts @@ -121,7 +121,7 @@ export class API { } async getFavorites(key?: string): Promise | Favorite | null> { - const url = key ? `/api/favorites?key=${key}` : "/api/favorites"; + const url = key ? `/api/favorites?key=${encodeURIComponent(key)}` : "/api/favorites"; const response = await this._fetch(url); return response.json(); } @@ -136,7 +136,7 @@ export class API { } async deleteFavorite(key?: string): Promise<{ success: boolean }> { - const url = key ? `/api/favorites?key=${key}` : "/api/favorites"; + const url = key ? `/api/favorites?key=${encodeURIComponent(key)}` : "/api/favorites"; const response = await this._fetch(url, { method: "DELETE" }); return response.json(); } @@ -156,7 +156,7 @@ export class API { } async deletePlayRecord(key?: string): Promise<{ success: boolean }> { - const url = key ? `/api/playrecords?key=${key}` : "/api/playrecords"; + const url = key ? `/api/playrecords?key=${encodeURIComponent(key)}` : "/api/playrecords"; const response = await this._fetch(url, { method: "DELETE" }); return response.json(); } diff --git a/stores/authStore.ts b/stores/authStore.ts index c2f7e5d..1bc6181 100644 --- a/stores/authStore.ts +++ b/stores/authStore.ts @@ -1,6 +1,7 @@ import { create } from "zustand"; import Cookies from "@react-native-cookies/cookies"; import { api } from "@/services/api"; +import { useSettingsStore } from "./settingsStore"; interface AuthState { isLoggedIn: boolean; @@ -22,11 +23,21 @@ const useAuthStore = create((set) => ({ return; } try { + const serverConfig = useSettingsStore.getState().serverConfig; const cookies = await Cookies.get(api.baseURL); - const isLoggedIn = cookies && !!cookies.auth; - set({ isLoggedIn }); - if (!isLoggedIn) { - set({ isLoginModalVisible: true }); + if (serverConfig && serverConfig.StorageType === "localstorage" && !cookies.auth) { + const loginResult = await api.login().catch(() => { + set({ isLoggedIn: false, isLoginModalVisible: true }); + }); + if (loginResult && loginResult.ok) { + set({ isLoggedIn: true }); + } + } else { + const isLoggedIn = cookies && !!cookies.auth; + set({ isLoggedIn }); + if (!isLoggedIn) { + set({ isLoginModalVisible: true }); + } } } catch (error) { console.info("Failed to check login status:", error);