Skip to content

Commit bb70978

Browse files
committed
✨ 完善账号功能
1 parent cacfc7f commit bb70978

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

app/src/core/service/User.tsx renamed to app/src/core/service/UserState.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { Store } from "@tauri-apps/plugin-store";
22
import { createStore } from "../../utils/store";
33
import { FeatureFlags } from "./FeatureFlags";
44

5-
export namespace User {
5+
export namespace UserState {
66
let store: Store;
77

88
export async function init() {
99
if (!FeatureFlags.USER) {
1010
return;
1111
}
12-
store = await createStore("user");
12+
store = await createStore("user.json");
1313
}
1414

1515
export async function getToken() {

app/src/main.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ import { createRoot } from "react-dom/client";
66
import { initReactI18next } from "react-i18next";
77
import { createMemoryRouter, RouterProvider } from "react-router-dom";
88
import { runCli } from "./cli";
9+
import { Dialog } from "./components/dialog";
910
import { UserScriptsManager } from "./core/plugin/UserScriptsManager";
1011
import { EdgeRenderer } from "./core/render/canvas2d/entityRenderer/edge/EdgeRenderer";
1112
import { Renderer } from "./core/render/canvas2d/renderer";
1213
import { InputElement } from "./core/render/domElement/inputElement";
1314
import { KeyboardOnlyEngine } from "./core/service/controlService/keyboardOnlyEngine/keyboardOnlyEngine";
1415
import { MouseLocation } from "./core/service/controlService/MouseLocation";
1516
import { KeyBinds } from "./core/service/controlService/shortcutKeysEngine/KeyBinds";
17+
import { ShortcutKeysRegister } from "./core/service/controlService/shortcutKeysEngine/shortcutKeysRegister";
18+
import { FileLoader } from "./core/service/dataFileService/fileLoader";
1619
import { RecentFileManager } from "./core/service/dataFileService/RecentFileManager";
1720
import { StartFilesManager } from "./core/service/dataFileService/StartFilesManager";
1821
import { ColorManager } from "./core/service/feedbackService/ColorManager";
@@ -22,6 +25,7 @@ import { StageStyleManager } from "./core/service/feedbackService/stageStyle/Sta
2225
import { LastLaunch } from "./core/service/LastLaunch";
2326
import { Settings } from "./core/service/Settings";
2427
import { Tourials } from "./core/service/Tourials";
28+
import { UserState } from "./core/service/UserState";
2529
import { Camera } from "./core/stage/Camera";
2630
import { Stage } from "./core/stage/Stage";
2731
import { StageLoader } from "./core/stage/StageLoader";
@@ -33,9 +37,6 @@ import "./polyfills/roundRect";
3337
import { exists } from "./utils/fs";
3438
import { exit, writeStderr } from "./utils/otherApi";
3539
import { getCurrentWindow, isDesktop, isFrame, isWeb, isWindows } from "./utils/platform";
36-
import { ShortcutKeysRegister } from "./core/service/controlService/shortcutKeysEngine/shortcutKeysRegister";
37-
import { FileLoader } from "./core/service/dataFileService/fileLoader";
38-
import { Dialog } from "./components/dialog";
3940

4041
const router = createMemoryRouter(routes);
4142
const Routes = () => <RouterProvider router={router} />;
@@ -56,6 +57,7 @@ const el = document.getElementById("root")!;
5657
ColorManager.init(),
5758
Tourials.init(),
5859
UserScriptsManager.init(),
60+
UserState.init(),
5961
]);
6062
// 这些东西依赖上面的东西,所以单独一个Promise.all
6163
await Promise.all([loadLanguageFiles(), loadSyncModules(), loadStartFile(), ShortcutKeysRegister.registerKeyBinds()]);

app/src/pages/user/login.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
import { Turnstile } from "@marsidev/react-turnstile";
1+
import { Turnstile, TurnstileInstance } from "@marsidev/react-turnstile";
22
import { fetch } from "@tauri-apps/plugin-http";
33
import { Check, Key, User } from "lucide-react";
4-
import { useState } from "react";
4+
import { useRef, useState } from "react";
55
import Button from "../../components/Button";
66
import { Dialog } from "../../components/dialog";
77
import Input from "../../components/Input";
88
import { Settings } from "../../core/service/Settings";
99
import { Themes } from "../../core/service/Themes";
10+
import { UserState } from "../../core/service/UserState";
1011

1112
export default function LoginPage() {
1213
const [theme] = Settings.use("theme");
1314
const [username, setUsername] = useState("");
1415
const [password, setPassword] = useState("");
1516
const [turnstileToken, setTurnstileToken] = useState("");
17+
const turnstileInstance = useRef<TurnstileInstance>(null);
1618

1719
const login = async () => {
1820
const resp = await fetch(`${import.meta.env.LR_API_BASE_URL}/user/login`, {
@@ -27,6 +29,7 @@ export default function LoginPage() {
2729
}),
2830
});
2931
if (resp.status === 200) {
32+
await UserState.setToken((await resp.json()).token);
3033
Dialog.show({
3134
type: "success",
3235
title: "登录",
@@ -39,6 +42,7 @@ export default function LoginPage() {
3942
content: (await resp.json()).msg,
4043
});
4144
}
45+
turnstileInstance.current?.reset();
4246
};
4347

4448
return (
@@ -54,6 +58,7 @@ export default function LoginPage() {
5458
</div>
5559
<Turnstile
5660
siteKey={import.meta.env.LR_TURNSTILE_SITE_KEY ?? ""}
61+
ref={turnstileInstance}
5762
onSuccess={setTurnstileToken}
5863
options={{
5964
theme: Themes.getThemeById(theme)?.metadata.type,

app/src/pages/user/register.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Turnstile } from "@marsidev/react-turnstile";
1+
import { Turnstile, TurnstileInstance } from "@marsidev/react-turnstile";
22
import { fetch } from "@tauri-apps/plugin-http";
33
import { open } from "@tauri-apps/plugin-shell";
44
import { Check, Key, Ticket, User } from "lucide-react";
5-
import { useState } from "react";
5+
import { useRef, useState } from "react";
66
import Button from "../../components/Button";
77
import { Dialog } from "../../components/dialog";
88
import Input from "../../components/Input";
@@ -16,6 +16,7 @@ export default function LoginPage() {
1616
const [passwordConfirm, setPasswordConfirm] = useState("");
1717
const [inviteCode, setInviteCode] = useState("");
1818
const [turnstileToken, setTurnstileToken] = useState("");
19+
const turnstileInstance = useRef<TurnstileInstance>(null);
1920

2021
const register = async () => {
2122
if (password !== passwordConfirm) {
@@ -50,6 +51,7 @@ export default function LoginPage() {
5051
content: (await resp.json()).msg,
5152
});
5253
}
54+
turnstileInstance.current?.reset();
5355
};
5456

5557
return (
@@ -80,6 +82,7 @@ export default function LoginPage() {
8082
</Button>
8183
<Turnstile
8284
siteKey={import.meta.env.LR_TURNSTILE_SITE_KEY ?? ""}
85+
ref={turnstileInstance}
8386
onSuccess={setTurnstileToken}
8487
options={{
8588
theme: Themes.getThemeById(theme)?.metadata.type,

0 commit comments

Comments
 (0)