From 57c23e21bbbe29948651479874f520d0422322cf Mon Sep 17 00:00:00 2001 From: songtianlun Date: Sat, 30 Aug 2025 12:09:21 +0800 Subject: [PATCH] fix username --- src/app/profile/page.tsx | 63 ++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 38 deletions(-) diff --git a/src/app/profile/page.tsx b/src/app/profile/page.tsx index 0f103bf..7205750 100644 --- a/src/app/profile/page.tsx +++ b/src/app/profile/page.tsx @@ -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(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 = { userId: user.id } + + // 用户名字段映射到name字段 + if (field === 'username') { + updateData['name'] = value // Better Auth的name字段 } else { - // Use our API for other fields - const updateData: Record = { 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() { ) : ( -

{profile?.username || t('noUsernameSet')}

+

{profile?.name || t('noUsernameSet')}

)}