Implement password change functionality and initialize superuser

This commit is contained in:
2026-01-26 16:41:22 +08:00
parent a3b69df2c2
commit 86247aa5a2
10 changed files with 368 additions and 38 deletions

View File

@@ -1,8 +1,13 @@
import { Menu, LogOut, Users } from 'lucide-react'
import { Menu, LogOut, Users, KeyRound } from 'lucide-react'
import { Link } from 'react-router-dom'
import { useState } from 'react'
import { toast } from 'sonner'
import { Button } from '@/components/ui/button'
import { ThemeToggle } from '@/components/ThemeToggle'
import { useAuth } from '@/contexts/AuthContext'
import { authApi } from '@/lib/api'
import { ChangePasswordDialog } from '@/components/users/ChangePasswordDialog'
import type { PasswordChangeRequest } from '@/types/auth'
interface NavbarProps {
onToggleSidebar?: () => void
@@ -10,41 +15,72 @@ interface NavbarProps {
export function Navbar({ onToggleSidebar }: NavbarProps) {
const { logout, user } = useAuth()
const [passwordDialogOpen, setPasswordDialogOpen] = useState(false)
const [isChangingPassword, setIsChangingPassword] = useState(false)
const handlePasswordChange = async (data: PasswordChangeRequest) => {
try {
setIsChangingPassword(true)
await authApi.changePassword(data)
toast.success('密码修改成功')
setPasswordDialogOpen(false)
} catch (error: any) {
toast.error(error.message || '密码修改失败')
} finally {
setIsChangingPassword(false)
}
}
return (
<nav className="h-16 border-b bg-background flex items-center px-4 gap-4">
{onToggleSidebar && (
<Button
variant="ghost"
size="icon"
onClick={onToggleSidebar}
className="lg:hidden"
>
<Menu className="h-5 w-5" />
</Button>
)}
<div className="flex-1">
<Link to="/">
<h1 className="text-sm md:text-xl font-bold cursor-pointer hover:opacity-80 transition-opacity">
Qwen3-TTS-WebUI
</h1>
</Link>
</div>
<div className="flex items-center gap-2">
{user?.is_superuser && (
<Link to="/users">
<Button variant="ghost" size="icon">
<Users className="h-5 w-5" />
</Button>
</Link>
<>
<nav className="h-16 border-b bg-background flex items-center px-4 gap-4">
{onToggleSidebar && (
<Button
variant="ghost"
size="icon"
onClick={onToggleSidebar}
className="lg:hidden"
>
<Menu className="h-5 w-5" />
</Button>
)}
<ThemeToggle />
<Button variant="ghost" size="icon" onClick={logout}>
<LogOut className="h-5 w-5" />
</Button>
</div>
</nav>
<div className="flex-1">
<Link to="/">
<h1 className="text-sm md:text-xl font-bold cursor-pointer hover:opacity-80 transition-opacity">
Qwen3-TTS-WebUI
</h1>
</Link>
</div>
<div className="flex items-center gap-2">
{user?.is_superuser && (
<Link to="/users">
<Button variant="ghost" size="icon">
<Users className="h-5 w-5" />
</Button>
</Link>
)}
<Button
variant="ghost"
size="icon"
onClick={() => setPasswordDialogOpen(true)}
>
<KeyRound className="h-5 w-5" />
</Button>
<ThemeToggle />
<Button variant="ghost" size="icon" onClick={logout}>
<LogOut className="h-5 w-5" />
</Button>
</div>
</nav>
<ChangePasswordDialog
open={passwordDialogOpen}
onOpenChange={setPasswordDialogOpen}
onSubmit={handlePasswordChange}
isLoading={isChangingPassword}
/>
</>
)
}

View File

@@ -0,0 +1,161 @@
import { useForm } from 'react-hook-form'
import { zodResolver } from '@hookform/resolvers/zod'
import * as z from 'zod'
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog'
import { Button } from '@/components/ui/button'
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from '@/components/ui/form'
import { Input } from '@/components/ui/input'
import type { PasswordChangeRequest } from '@/types/auth'
const passwordChangeSchema = z.object({
current_password: z.string().min(1, '请输入当前密码'),
new_password: z
.string()
.min(8, '密码至少8个字符')
.regex(/[A-Z]/, '密码必须包含至少一个大写字母')
.regex(/[a-z]/, '密码必须包含至少一个小写字母')
.regex(/\d/, '密码必须包含至少一个数字'),
confirm_password: z.string().min(1, '请确认新密码'),
}).refine((data) => data.new_password === data.confirm_password, {
message: '两次输入的密码不一致',
path: ['confirm_password'],
})
type PasswordChangeFormValues = z.infer<typeof passwordChangeSchema>
interface ChangePasswordDialogProps {
open: boolean
onOpenChange: (open: boolean) => void
onSubmit: (data: PasswordChangeRequest) => Promise<void>
isLoading: boolean
}
export function ChangePasswordDialog({
open,
onOpenChange,
onSubmit,
isLoading,
}: ChangePasswordDialogProps) {
const form = useForm<PasswordChangeFormValues>({
resolver: zodResolver(passwordChangeSchema),
defaultValues: {
current_password: '',
new_password: '',
confirm_password: '',
},
})
const handleSubmit = async (data: PasswordChangeFormValues) => {
await onSubmit(data)
form.reset()
}
const handleOpenChange = (open: boolean) => {
if (!open && !isLoading) {
form.reset()
}
onOpenChange(open)
}
return (
<Dialog open={open} onOpenChange={handleOpenChange}>
<DialogContent className="sm:max-w-[425px]">
<DialogHeader>
<DialogTitle></DialogTitle>
<DialogDescription>
8
</DialogDescription>
</DialogHeader>
<Form {...form}>
<form onSubmit={form.handleSubmit(handleSubmit)} className="space-y-4">
<FormField
control={form.control}
name="current_password"
render={({ field }) => (
<FormItem>
<FormLabel></FormLabel>
<FormControl>
<Input
type="password"
placeholder="请输入当前密码"
disabled={isLoading}
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="new_password"
render={({ field }) => (
<FormItem>
<FormLabel></FormLabel>
<FormControl>
<Input
type="password"
placeholder="请输入新密码"
disabled={isLoading}
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="confirm_password"
render={({ field }) => (
<FormItem>
<FormLabel></FormLabel>
<FormControl>
<Input
type="password"
placeholder="请再次输入新密码"
disabled={isLoading}
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<DialogFooter>
<Button
type="button"
variant="outline"
onClick={() => handleOpenChange(false)}
disabled={isLoading}
>
</Button>
<Button type="submit" disabled={isLoading}>
{isLoading ? '提交中...' : '确认修改'}
</Button>
</DialogFooter>
</form>
</Form>
</DialogContent>
</Dialog>
)
}

View File

@@ -1,5 +1,5 @@
import axios from 'axios'
import type { LoginRequest, LoginResponse, User } from '@/types/auth'
import type { LoginRequest, LoginResponse, User, PasswordChangeRequest } from '@/types/auth'
import type { Job, JobCreateResponse, JobListResponse, JobType } from '@/types/job'
import type { Language, Speaker, CustomVoiceForm, VoiceDesignForm, VoiceCloneForm } from '@/types/tts'
import type { UserCreateRequest, UserUpdateRequest, UserListResponse } from '@/types/user'
@@ -32,6 +32,9 @@ const FIELD_NAMES: Record<string, string> = {
username: '用户名',
email: '邮箱',
password: '密码',
current_password: '当前密码',
new_password: '新密码',
confirm_password: '确认密码',
is_active: '激活状态',
is_superuser: '超级管理员',
}
@@ -174,6 +177,14 @@ export const authApi = {
const response = await apiClient.get<User>(API_ENDPOINTS.AUTH.ME)
return response.data
},
changePassword: async (data: PasswordChangeRequest): Promise<User> => {
const response = await apiClient.post<User>(
API_ENDPOINTS.AUTH.CHANGE_PASSWORD,
data
)
return response.data
},
}
export const ttsApi = {

View File

@@ -2,6 +2,7 @@ export const API_ENDPOINTS = {
AUTH: {
LOGIN: '/auth/token',
ME: '/auth/me',
CHANGE_PASSWORD: '/auth/change-password',
},
TTS: {
LANGUAGES: '/tts/languages',

View File

@@ -23,3 +23,9 @@ export interface AuthState {
isLoading: boolean
isAuthenticated: boolean
}
export interface PasswordChangeRequest {
current_password: string
new_password: string
confirm_password: string
}