From 5feb6c2bb67a85755104c43428933da61d3f1289 Mon Sep 17 00:00:00 2001 From: DanCodes Date: Thu, 11 Jan 2024 11:32:08 +0000 Subject: [PATCH 1/2] feat: store access token instead of password --- src/main.tsx | 42 ++++++++++++++++++++------------------ src/routes/login/index.jsx | 4 ++-- src/utils/storage/user.ts | 6 +++--- 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/main.tsx b/src/main.tsx index aa87bbc..c497810 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -1,5 +1,10 @@ import { Api } from "@jellyfin/sdk"; -import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api"; +import { + QueryClient, + QueryClientProvider, + useQuery, +} from "@tanstack/react-query"; import { enqueueSnackbar } from "notistack"; import React from "react"; import ReactDOM from "react-dom/client"; @@ -33,24 +38,12 @@ const handleAuthError = async () => { setInitialRoute("/login/index"); }; -const authenticateUser = async ( - address: string, - api: Api, - user: UserStore["user"], -) => { +const authenticateUser = async (address: string, user: UserStore["user"]) => { try { - const auth = await api.authenticateUserByName(user.Name, user.Password); + console.log(user); + createApi(address, user.AccessToken); - if (auth.status !== 200 || !auth.data.AccessToken) { - enqueueSnackbar("Incorrect Username or Password!", { - variant: "error", - }); - - handleAuthError(); - } else { - createApi(address, auth.data.AccessToken); - setInitialRoute("/home"); - } + setInitialRoute("/home"); } catch (error) { console.error(error); setInitialRoute("/error"); @@ -64,7 +57,7 @@ const init = async () => { } const defaultServerOnDisk = await getDefaultServer(); - console.log(defaultServerOnDisk) + console.log(defaultServerOnDisk); if (defaultServerOnDisk) { const defaultServerInfo = await getServer(defaultServerOnDisk); @@ -84,14 +77,23 @@ const init = async () => { if (userOnDisk) { try { - const authApi = useApi.getState().api; + let authApi = useApi.getState().api; if (!authApi) { handleAuthError(); return; } - await authenticateUser(defaultServerInfo.address, authApi, userOnDisk); + await authenticateUser(defaultServerInfo.address, userOnDisk); + + authApi = useApi.getState().api!; + + const user = await getUserApi(authApi).getCurrentUser(); + + if (!user) { + await delUser(); + handleAuthError(); + } } catch (error) { console.error(error); handleAuthError(); diff --git a/src/routes/login/index.jsx b/src/routes/login/index.jsx index e020407..3b82ef9 100644 --- a/src/routes/login/index.jsx +++ b/src/routes/login/index.jsx @@ -122,7 +122,7 @@ export const LoginWithImage = () => { const user = await authUser(); sessionStorage.setItem("accessToken", user.data.AccessToken); if (rememberMe === true) { - saveUser(userName, password.password); + saveUser(userName, user.data.AccessToken); } event.emit("set-api-accessToken", api.basePath); setLoading(false); @@ -435,7 +435,7 @@ export const UserLoginManual = () => { const user = await authUser(); sessionStorage.setItem("accessToken", user.data.AccessToken); if (rememberMe === true) { - saveUser(userName, password.password); + saveUser(userName, user.data.AccessToken); } event.emit("set-api-accessToken", api.basePath); // setAccessToken(user.data.AccessToken) diff --git a/src/utils/storage/user.ts b/src/utils/storage/user.ts index f5a484f..ba5a667 100644 --- a/src/utils/storage/user.ts +++ b/src/utils/storage/user.ts @@ -3,7 +3,7 @@ import { Store } from "tauri-plugin-store-api"; export interface UserStore { user: { Name: string; - Password: string; + AccessToken: string; }; } @@ -12,10 +12,10 @@ const user = new Store(".user.dat"); /** * Set User details to .user.dat */ -const saveUser = async (userName: string, userPassword?: string) => { +const saveUser = async (userName: string, accessToken: string) => { user.set("user", { Name: userName, - Password: userPassword, + AccessToken: accessToken, }); await user.save(); From 4acf57cfd040a7a20319d858a911c22518eb2156 Mon Sep 17 00:00:00 2001 From: DanCodes Date: Thu, 11 Jan 2024 14:51:37 +0000 Subject: [PATCH 2/2] chore: cleanup code --- src/App.tsx | 1 - src/main.tsx | 9 +-------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index c1267d3..51b3abc 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -161,7 +161,6 @@ function App() { undefined, ); - // biome-ignore lint/correctness/useExhaustiveDependencies: this dependency is required useEffect(() => { window.scrollTo(0, 0); }, [location.key]); diff --git a/src/main.tsx b/src/main.tsx index c497810..7230283 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -1,11 +1,5 @@ -import { Api } from "@jellyfin/sdk"; import { getUserApi } from "@jellyfin/sdk/lib/utils/api/user-api"; -import { - QueryClient, - QueryClientProvider, - useQuery, -} from "@tanstack/react-query"; -import { enqueueSnackbar } from "notistack"; +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; import React from "react"; import ReactDOM from "react-dom/client"; import { BrowserRouter as Router } from "react-router-dom"; @@ -40,7 +34,6 @@ const handleAuthError = async () => { const authenticateUser = async (address: string, user: UserStore["user"]) => { try { - console.log(user); createApi(address, user.AccessToken); setInitialRoute("/home");