diff --git a/apps/expo/src/app/(tabs)/movie-web.tsx b/apps/expo/src/app/(tabs)/movie-web.tsx index 6644026..0479195 100644 --- a/apps/expo/src/app/(tabs)/movie-web.tsx +++ b/apps/expo/src/app/(tabs)/movie-web.tsx @@ -1,4 +1,3 @@ -import { useState } from "react"; import { Link } from "expo-router"; import { H2, H5, Paragraph, View } from "tamagui"; @@ -6,9 +5,10 @@ import ScreenLayout from "~/components/layout/ScreenLayout"; import { MWButton } from "~/components/ui/Button"; import { MWCard } from "~/components/ui/Card"; import { MWInput } from "~/components/ui/Input"; +import { useAuthStore } from "~/stores/settings"; export default function MovieWebScreen() { - const [url, setUrl] = useState("https://mw-backend.lonelil.ru"); + const { backendUrl, setBackendUrl } = useAuthStore(); return ( @@ -46,7 +46,7 @@ export default function MovieWebScreen() { diff --git a/apps/expo/src/stores/settings/index.ts b/apps/expo/src/stores/settings/index.ts index 279c327..e040003 100644 --- a/apps/expo/src/stores/settings/index.ts +++ b/apps/expo/src/stores/settings/index.ts @@ -257,3 +257,66 @@ export const useNetworkSettingsStore = create< }, ), ); + +export interface Account { + profile: { + colorA: string; + colorB: string; + icon: string; + }; +} + +export type AccountWithToken = Account & { + sessionId: string; + userId: string; + token: string; + seed: string; + deviceName: string; +}; + +interface AuthStoreState { + account: null | AccountWithToken; + backendUrl: string; + proxySet: null | string[]; + removeAccount(): void; + setAccount(acc: AccountWithToken): void; + updateDeviceName(deviceName: string): void; + updateAccount(acc: Account): void; + setAccountProfile(acc: Account["profile"]): void; + setBackendUrl(url: string): void; + setProxySet(urls: null | string[]): void; +} + +export const useAuthStore = create< + AuthStoreState, + [["zustand/persist", AuthStoreState]] +>( + persist( + (set) => ({ + account: null, + backendUrl: "https://mw-backend.lonelil.ru", + proxySet: null, + setAccount: (acc) => set((s) => ({ ...s, account: acc })), + removeAccount: () => set((s) => ({ ...s, account: null })), + setBackendUrl: (v) => set((s) => ({ ...s, backendUrl: v })), + setProxySet: (urls) => set((s) => ({ ...s, proxySet: urls })), + setAccountProfile: (profile) => + set((s) => ({ + ...s, + account: s.account ? { ...s.account, profile } : s.account, + })), + updateAccount: (acc) => + set((s) => + s.account ? { ...s, account: { ...s.account, ...acc } } : s, + ), + updateDeviceName: (deviceName) => + set((s) => + s.account ? { ...s, account: { ...s.account, deviceName } } : s, + ), + }), + { + name: "account-settings", + storage: createJSONStorage(() => zustandStorage), + }, + ), +); diff --git a/packages/api/src/auth.ts b/packages/api/src/auth.ts index e6dbffb..71b2317 100644 --- a/packages/api/src/auth.ts +++ b/packages/api/src/auth.ts @@ -1,35 +1,35 @@ import { ofetch } from "ofetch"; export interface SessionResponse { - id: string; - userId: string; - createdAt: string; - accessedAt: string; - device: string; - userAgent: string; + id: string; + userId: string; + createdAt: string; + accessedAt: string; + device: string; + userAgent: string; } export interface LoginResponse { - session: SessionResponse; - token: string; + session: SessionResponse; + token: string; } export function getAuthHeaders(token: string): Record { - return { - authorization: `Bearer ${token}`, - }; + return { + authorization: `Bearer ${token}`, + }; } export async function accountLogin( - url: string, - id: string, - deviceName: string, + url: string, + id: string, + deviceName: string, ): Promise { - return ofetch("/auth/login", { - method: "POST", - body: { - id, - device: deviceName, - }, - baseURL: url, - }); -} \ No newline at end of file + return ofetch("/auth/login", { + method: "POST", + body: { + id, + device: deviceName, + }, + baseURL: url, + }); +}