feat: auth store

This commit is contained in:
Adrian Castro
2024-04-15 20:49:21 +02:00
parent e8dfb5eaf4
commit 4e01f35458
3 changed files with 92 additions and 29 deletions

View File

@@ -1,4 +1,3 @@
import { useState } from "react";
import { Link } from "expo-router"; import { Link } from "expo-router";
import { H2, H5, Paragraph, View } from "tamagui"; import { H2, H5, Paragraph, View } from "tamagui";
@@ -6,9 +5,10 @@ import ScreenLayout from "~/components/layout/ScreenLayout";
import { MWButton } from "~/components/ui/Button"; import { MWButton } from "~/components/ui/Button";
import { MWCard } from "~/components/ui/Card"; import { MWCard } from "~/components/ui/Card";
import { MWInput } from "~/components/ui/Input"; import { MWInput } from "~/components/ui/Input";
import { useAuthStore } from "~/stores/settings";
export default function MovieWebScreen() { export default function MovieWebScreen() {
const [url, setUrl] = useState("https://mw-backend.lonelil.ru"); const { backendUrl, setBackendUrl } = useAuthStore();
return ( return (
<ScreenLayout <ScreenLayout
@@ -34,10 +34,10 @@ export default function MovieWebScreen() {
<View padding="$4"> <View padding="$4">
<MWInput <MWInput
placeholder="https://mw-backend.lonelil.ru" placeholder={backendUrl}
type="search" type="search"
value={url} value={backendUrl}
onChangeText={setUrl} onChangeText={setBackendUrl}
/> />
</View> </View>
@@ -46,7 +46,7 @@ export default function MovieWebScreen() {
<Link <Link
href={{ href={{
pathname: "/sync/trust/[url]", pathname: "/sync/trust/[url]",
params: { url }, params: { url: backendUrl },
}} }}
style={{ color: "white", fontWeight: "bold" }} style={{ color: "white", fontWeight: "bold" }}
> >

View File

@@ -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),
},
),
);