This commit is contained in:
@@ -8,7 +8,7 @@ import { useSession, signOut, updateUser } from '../lib/auth-client';
|
||||
import { validatePhoneOrEmail } from '../lib/validators';
|
||||
|
||||
export function ProfileSettings() {
|
||||
const { data: session } = useSession();
|
||||
const { data: session, refetch: refetchSession } = useSession();
|
||||
const [userName, setUserName] = useState('');
|
||||
const [savingName, setSavingName] = useState(false);
|
||||
const [nameSaved, setNameSaved] = useState(false);
|
||||
@@ -48,11 +48,18 @@ export function ProfileSettings() {
|
||||
setNameSaved(false);
|
||||
const trimmed = userName.trim();
|
||||
setSavingName(true);
|
||||
const { error: err } = await updateUser({ name: trimmed });
|
||||
const res = await fetch('/api/account/name', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
credentials: 'include',
|
||||
body: JSON.stringify({ name: trimmed || null }),
|
||||
});
|
||||
setSavingName(false);
|
||||
if (err) {
|
||||
setNameError(err.message || 'Failed to save name.');
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
setNameError(data.error || 'Failed to save name.');
|
||||
} else {
|
||||
await refetchSession();
|
||||
setNameSaved(true);
|
||||
setTimeout(() => setNameSaved(false), 3000);
|
||||
}
|
||||
|
||||
@@ -11,6 +11,18 @@ const uploadsDir = process.env.UPLOADS_DIR || path.join(__dirname, '../../upload
|
||||
const router = Router();
|
||||
router.use(requireAuth);
|
||||
|
||||
// POST /api/account/name — update (or clear) the authenticated user's display name
|
||||
router.post('/name', async (req, res) => {
|
||||
const { name } = req.body;
|
||||
const cleanName = typeof name === 'string' && name.trim() !== '' ? name.trim() : null;
|
||||
try {
|
||||
await pool.query('UPDATE "user" SET name = $1 WHERE id = $2', [cleanName, req.user!.id]);
|
||||
res.json({ name: cleanName });
|
||||
} catch {
|
||||
res.status(500).json({ error: 'Failed to update name' });
|
||||
}
|
||||
});
|
||||
|
||||
// POST /api/account/delete
|
||||
router.post('/delete', async (req, res) => {
|
||||
const userId = req.user!.id;
|
||||
|
||||
Reference in New Issue
Block a user