fix username
This commit is contained in:
parent
08234443c1
commit
57c23e21bb
@ -3,7 +3,6 @@
|
||||
import { useState, useEffect, useCallback } from 'react'
|
||||
import { useTranslations } from 'next-intl'
|
||||
import { useAuthUser } from '@/hooks/useAuthUser'
|
||||
import { createClient } from '@/lib/supabase'
|
||||
import { Header } from '@/components/layout/Header'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
@ -83,7 +82,6 @@ export default function ProfilePage() {
|
||||
const [fieldLoading, setFieldLoading] = useState<{ [key: string]: boolean }>({})
|
||||
const [creditInfo, setCreditInfo] = useState<CreditInfo | null>(null)
|
||||
|
||||
const supabase = createClient()
|
||||
|
||||
const loadProfile = useCallback(async () => {
|
||||
if (!user) return
|
||||
@ -104,7 +102,7 @@ export default function ProfilePage() {
|
||||
|
||||
setProfile(profileData)
|
||||
setFormData({
|
||||
username: profileData.username || '',
|
||||
username: profileData.name || '', // 直接使用name字段
|
||||
email: profileData.email,
|
||||
bio: profileData.bio || '',
|
||||
currentPassword: '',
|
||||
@ -139,29 +137,29 @@ export default function ProfilePage() {
|
||||
setSaveStatus({ type: null, message: '' })
|
||||
|
||||
try {
|
||||
if (field === 'email') {
|
||||
const { error } = await supabase.auth.updateUser({ email: value as string })
|
||||
if (error) throw error
|
||||
setSaveStatus({ type: 'success', message: t('checkEmailToConfirm') })
|
||||
// Use our API for all fields
|
||||
const updateData: Record<string, unknown> = { userId: user.id }
|
||||
|
||||
// 用户名字段映射到name字段
|
||||
if (field === 'username') {
|
||||
updateData['name'] = value // Better Auth的name字段
|
||||
} else {
|
||||
// Use our API for other fields
|
||||
const updateData: Record<string, unknown> = { userId: user.id }
|
||||
updateData[field] = value
|
||||
|
||||
const response = await fetch('/api/users/profile', {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(updateData)
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json()
|
||||
throw new Error(errorData.error || 'Failed to update profile')
|
||||
}
|
||||
|
||||
setSaveStatus({ type: 'success', message: `${field} ${t('updatedSuccessfully')}` })
|
||||
}
|
||||
|
||||
const response = await fetch('/api/users/profile', {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(updateData)
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json()
|
||||
throw new Error(errorData.error || 'Failed to update profile')
|
||||
}
|
||||
|
||||
setSaveStatus({ type: 'success', message: `${field} ${t('updatedSuccessfully')}` })
|
||||
|
||||
// Reload profile and trigger cache update
|
||||
await Promise.all([
|
||||
loadProfile(),
|
||||
@ -195,20 +193,9 @@ export default function ProfilePage() {
|
||||
setSaveStatus({ type: null, message: '' })
|
||||
|
||||
try {
|
||||
const { error } = await supabase.auth.updateUser({
|
||||
password: formData.newPassword
|
||||
})
|
||||
|
||||
if (error) throw error
|
||||
|
||||
setSaveStatus({ type: 'success', message: t('passwordUpdatedSuccessfully') })
|
||||
setFormData(prev => ({
|
||||
...prev,
|
||||
currentPassword: '',
|
||||
newPassword: '',
|
||||
confirmPassword: ''
|
||||
}))
|
||||
setIsEditing(prev => ({ ...prev, password: false }))
|
||||
// TODO: 实现Better Auth密码更新
|
||||
// Better Auth的密码更新需要通过特殊的API端点
|
||||
setSaveStatus({ type: 'error', message: 'Password update is not yet implemented for Better Auth' })
|
||||
|
||||
} catch (error: unknown) {
|
||||
setSaveStatus({ type: 'error', message: (error instanceof Error ? error.message : 'Unknown error') || t('failedToUpdatePassword') })
|
||||
@ -457,7 +444,7 @@ export default function ProfilePage() {
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
setIsEditing(prev => ({ ...prev, username: false }))
|
||||
setFormData(prev => ({ ...prev, username: profile?.username || '' }))
|
||||
setFormData(prev => ({ ...prev, username: profile?.name || '' }))
|
||||
}}
|
||||
disabled={fieldLoading.username}
|
||||
>
|
||||
@ -466,7 +453,7 @@ export default function ProfilePage() {
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<p className="text-foreground">{profile?.username || t('noUsernameSet')}</p>
|
||||
<p className="text-foreground">{profile?.name || t('noUsernameSet')}</p>
|
||||
)}
|
||||
</div>
|
||||
</LoadingOverlay>
|
||||
|
Loading…
Reference in New Issue
Block a user