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 { 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 (
|
||||
<ScreenLayout
|
||||
@@ -34,10 +34,10 @@ export default function MovieWebScreen() {
|
||||
|
||||
<View padding="$4">
|
||||
<MWInput
|
||||
placeholder="https://mw-backend.lonelil.ru"
|
||||
placeholder={backendUrl}
|
||||
type="search"
|
||||
value={url}
|
||||
onChangeText={setUrl}
|
||||
value={backendUrl}
|
||||
onChangeText={setBackendUrl}
|
||||
/>
|
||||
</View>
|
||||
|
||||
@@ -46,7 +46,7 @@ export default function MovieWebScreen() {
|
||||
<Link
|
||||
href={{
|
||||
pathname: "/sync/trust/[url]",
|
||||
params: { url },
|
||||
params: { url: backendUrl },
|
||||
}}
|
||||
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";
|
||||
|
||||
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<string, string> {
|
||||
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<LoginResponse> {
|
||||
return ofetch<LoginResponse>("/auth/login", {
|
||||
method: "POST",
|
||||
body: {
|
||||
id,
|
||||
device: deviceName,
|
||||
},
|
||||
baseURL: url,
|
||||
});
|
||||
}
|
||||
return ofetch<LoginResponse>("/auth/login", {
|
||||
method: "POST",
|
||||
body: {
|
||||
id,
|
||||
device: deviceName,
|
||||
},
|
||||
baseURL: url,
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user