feat(client): 登录注册
This commit is contained in:
@@ -2,16 +2,17 @@ import isMobileUI from "../utils/isMobileUI.ts"
|
||||
import useEventListener from "../utils/useEventListener.ts"
|
||||
import AvatarMySelf from "./AvatarMySelf.tsx"
|
||||
import MainSharedContext from './MainSharedContext.ts'
|
||||
import React from "react"
|
||||
import * as React from 'react'
|
||||
import { BrowserRouter, Outlet, Route, Routes } from "react-router"
|
||||
import LoginDialog from "./main-page/LoginDialog.tsx"
|
||||
import useAsyncEffect from "../utils/useAsyncEffect.ts"
|
||||
import performAuth from "../performAuth.ts"
|
||||
import { CallbackError } from "lingchair-client-protocol"
|
||||
import showCircleProgressDialog from "./showCircleProgressDialog.ts"
|
||||
import RegisterDialog from "./main-page/RegisterDialog.tsx"
|
||||
import sleep from "../utils/sleep.ts"
|
||||
|
||||
export default function Main() {
|
||||
const [showLoginDialog, setShowLoginDialog] = React.useState(false)
|
||||
|
||||
// 多页面切换
|
||||
const navigationRef = React.useRef<HTMLElement>()
|
||||
const [currentShowPage, setCurrentShowPage] = React.useState('Recents')
|
||||
@@ -20,14 +21,19 @@ export default function Main() {
|
||||
setCurrentShowPage((event.target as HTMLElementWithValue).value)
|
||||
})
|
||||
|
||||
const [showLoginDialog, setShowLoginDialog] = React.useState(false)
|
||||
const [showRegisterDialog, setShowRegisterDialog] = React.useState(false)
|
||||
|
||||
const sharedContext = {
|
||||
ui_functions: React.useRef({
|
||||
|
||||
}),
|
||||
setShowLoginDialog,
|
||||
setShowRegisterDialog,
|
||||
}
|
||||
|
||||
useAsyncEffect(async () => {
|
||||
const waitingForAuth = showCircleProgressDialog("验证中...")
|
||||
try {
|
||||
await performAuth({})
|
||||
} catch (e) {
|
||||
@@ -35,6 +41,9 @@ export default function Main() {
|
||||
if (e.code == 401 || e.code == 400)
|
||||
setShowLoginDialog(true)
|
||||
}
|
||||
// 动画都没来得及, 稍微等一下 (
|
||||
await sleep(100)
|
||||
waitingForAuth.open = false
|
||||
})
|
||||
|
||||
return (
|
||||
@@ -53,6 +62,7 @@ export default function Main() {
|
||||
<Outlet />
|
||||
}
|
||||
<LoginDialog open={showLoginDialog} />
|
||||
<RegisterDialog open={showRegisterDialog} />
|
||||
{
|
||||
/**
|
||||
* Default: 侧边列表提供列表切换
|
||||
|
||||
@@ -2,17 +2,27 @@ import * as React from 'react'
|
||||
import { Button, Dialog, TextField } from "mdui"
|
||||
|
||||
import performAuth from '../../performAuth.ts'
|
||||
import useEventListener from '../../utils/useEventListener.ts'
|
||||
import showSnackbar from '../../utils/showSnackbar.ts'
|
||||
import MainSharedContext from '../MainSharedContext.ts'
|
||||
|
||||
export default function LoginDialog({ ...props }: { open: boolean } & React.HTMLAttributes<Dialog>) {
|
||||
const shared = React.useContext(MainSharedContext)
|
||||
|
||||
const loginDialogRef = React.useRef<Dialog>(null)
|
||||
const loginButtonRef = React.useRef<Button>(null)
|
||||
const registerButtonRef = React.useRef<Button>(null)
|
||||
const loginInputAccountRef = React.useRef<TextField>(null)
|
||||
const loginInputPasswordRef = React.useRef<TextField>(null)
|
||||
|
||||
useEventListener(loginButtonRef, 'click', async () => {
|
||||
return (
|
||||
<mdui-dialog {...props} headline="登录" ref={loginDialogRef}>
|
||||
|
||||
<mdui-text-field label="用户 ID / 用户名" ref={loginInputAccountRef}></mdui-text-field>
|
||||
<div style={{
|
||||
height: "10px",
|
||||
}}></div>
|
||||
<mdui-text-field label="密码" type="password" toggle-password ref={loginInputPasswordRef}></mdui-text-field>
|
||||
|
||||
<mdui-button slot="action" variant="text" onClick={() => shared.setShowRegisterDialog(true)}>注册</mdui-button>
|
||||
<mdui-button slot="action" variant="text" onClick={async () => {
|
||||
const account = loginInputAccountRef.current!.value
|
||||
const password = loginInputPasswordRef.current!.value
|
||||
|
||||
@@ -26,19 +36,7 @@ export default function LoginDialog({ ...props }: { open: boolean } & React.HTML
|
||||
if (e instanceof Error)
|
||||
showSnackbar({ message: '登录失败: ' + e.message })
|
||||
}
|
||||
})
|
||||
|
||||
return (
|
||||
<mdui-dialog {...props} headline="登录" ref={loginDialogRef}>
|
||||
|
||||
<mdui-text-field label="用户 ID / 用户名" ref={loginInputAccountRef}></mdui-text-field>
|
||||
<div style={{
|
||||
height: "10px",
|
||||
}}></div>
|
||||
<mdui-text-field label="密码" type="password" toggle-password ref={loginInputPasswordRef}></mdui-text-field>
|
||||
|
||||
<mdui-button slot="action" variant="text" ref={registerButtonRef}>注册</mdui-button>
|
||||
<mdui-button slot="action" variant="text" ref={loginButtonRef}>登录</mdui-button>
|
||||
}}>登录</mdui-button>
|
||||
</mdui-dialog>
|
||||
)
|
||||
}
|
||||
69
client/ui/main-page/RegisterDialog.tsx
Normal file
69
client/ui/main-page/RegisterDialog.tsx
Normal file
@@ -0,0 +1,69 @@
|
||||
import * as React from 'react'
|
||||
import { Button, Dialog, TextField } from "mdui"
|
||||
import MainSharedContext from '../MainSharedContext'
|
||||
import showSnackbar from '../../utils/showSnackbar'
|
||||
import showCircleProgressDialog from '../showCircleProgressDialog'
|
||||
import getClient from '../../getClient'
|
||||
import performAuth from '../../performAuth'
|
||||
|
||||
export default function RegisterDialog({ ...props }: { open: boolean } & React.HTMLAttributes<Dialog>) {
|
||||
const shared = React.useContext(MainSharedContext)
|
||||
|
||||
const registerInputUserNameRef = React.useRef<TextField>(null)
|
||||
const registerInputNickNameRef = React.useRef<TextField>(null)
|
||||
const registerInputPasswordRef = React.useRef<TextField>(null)
|
||||
|
||||
return (
|
||||
<mdui-dialog headline="注册" {...props}>
|
||||
|
||||
<mdui-text-field label="用户名 (可选)" ref={registerInputUserNameRef}></mdui-text-field>
|
||||
<div style={{
|
||||
height: "10px",
|
||||
}}></div>
|
||||
<mdui-text-field label="昵称" ref={registerInputNickNameRef}></mdui-text-field>
|
||||
<div style={{
|
||||
height: "10px",
|
||||
}}></div>
|
||||
<mdui-text-field label="密码" type="password" toggle-password ref={registerInputPasswordRef}></mdui-text-field>
|
||||
|
||||
<mdui-button slot="action" variant="text" onClick={() => shared.setShowRegisterDialog(false)}>返回</mdui-button>
|
||||
<mdui-button slot="action" variant="text" onClick={async () => {
|
||||
const waitingForRegister = showCircleProgressDialog("注册中...")
|
||||
|
||||
const username = registerInputUserNameRef.current!.value
|
||||
|
||||
let user_id: string
|
||||
try {
|
||||
user_id = await getClient().registerOrThrow({
|
||||
username: username,
|
||||
nickname: registerInputNickNameRef.current!.value,
|
||||
password: registerInputPasswordRef.current!.value,
|
||||
})
|
||||
} catch (e) {
|
||||
user_id = ''
|
||||
if (e instanceof Error) {
|
||||
waitingForRegister.open = false
|
||||
showSnackbar({ message: '注册失败: ' + e.message })
|
||||
return
|
||||
}
|
||||
}
|
||||
waitingForRegister.open = false
|
||||
|
||||
const waitingForLogin = showCircleProgressDialog("登录中...")
|
||||
|
||||
try {
|
||||
await performAuth({
|
||||
account: username == '' ? username : user_id,
|
||||
password: registerInputPasswordRef.current!.value,
|
||||
})
|
||||
location.reload()
|
||||
} catch (e) {
|
||||
if (e instanceof Error) {
|
||||
showSnackbar({ message: '登录失败: ' + e.message })
|
||||
}
|
||||
}
|
||||
waitingForLogin.open = false
|
||||
}}>注册</mdui-button>
|
||||
</mdui-dialog>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user