mirror of
https://github.com/movie-web/native-app.git
synced 2025-09-13 10:23:24 +00:00
feat: auth store
This commit is contained in:
@@ -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" }}
|
||||||
>
|
>
|
||||||
|
@@ -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),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
@@ -1,35 +1,35 @@
|
|||||||
import { ofetch } from "ofetch";
|
import { ofetch } from "ofetch";
|
||||||
|
|
||||||
export interface SessionResponse {
|
export interface SessionResponse {
|
||||||
id: string;
|
id: string;
|
||||||
userId: string;
|
userId: string;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
accessedAt: string;
|
accessedAt: string;
|
||||||
device: string;
|
device: string;
|
||||||
userAgent: string;
|
userAgent: string;
|
||||||
}
|
}
|
||||||
export interface LoginResponse {
|
export interface LoginResponse {
|
||||||
session: SessionResponse;
|
session: SessionResponse;
|
||||||
token: string;
|
token: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getAuthHeaders(token: string): Record<string, string> {
|
export function getAuthHeaders(token: string): Record<string, string> {
|
||||||
return {
|
return {
|
||||||
authorization: `Bearer ${token}`,
|
authorization: `Bearer ${token}`,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function accountLogin(
|
export async function accountLogin(
|
||||||
url: string,
|
url: string,
|
||||||
id: string,
|
id: string,
|
||||||
deviceName: string,
|
deviceName: string,
|
||||||
): Promise<LoginResponse> {
|
): Promise<LoginResponse> {
|
||||||
return ofetch<LoginResponse>("/auth/login", {
|
return ofetch<LoginResponse>("/auth/login", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: {
|
body: {
|
||||||
id,
|
id,
|
||||||
device: deviceName,
|
device: deviceName,
|
||||||
},
|
},
|
||||||
baseURL: url,
|
baseURL: url,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user