Fix settings validation and session errors

This commit is contained in:
gpt-engineer-app[bot]
2025-10-14 23:48:34 +00:00
parent 439a7f4abf
commit 21d16f01ed
5 changed files with 62 additions and 29 deletions

View File

@@ -60,19 +60,24 @@ export function PrivacyTab() {
} }
if (data?.privacy_settings) { if (data?.privacy_settings) {
// Validate the data before using it const parseResult = privacySettingsSchema.safeParse(data.privacy_settings);
const validatedSettings = privacySettingsSchema.parse(data.privacy_settings);
setPreferences(validatedSettings);
if (parseResult.success) {
setPreferences(parseResult.data);
form.reset({ form.reset({
privacy_level: (profile?.privacy_level === 'public' || profile?.privacy_level === 'private') privacy_level: (profile?.privacy_level === 'public' || profile?.privacy_level === 'private')
? profile.privacy_level ? profile.privacy_level
: 'public', : 'public',
show_pronouns: profile?.show_pronouns || false, show_pronouns: profile?.show_pronouns || false,
...validatedSettings ...parseResult.data
}); });
} else { } else {
// Initialize preferences if they don't exist console.warn('Invalid privacy settings, reinitializing with defaults', {
errors: parseResult.error.issues
});
await initializePreferences();
}
} else {
await initializePreferences(); await initializePreferences();
} }
} catch (error) { } catch (error) {

View File

@@ -146,19 +146,30 @@ export function SecurityTab() {
const fetchSessions = async () => { const fetchSessions = async () => {
if (!user) return; if (!user) return;
setLoadingSessions(true);
try {
const { data, error } = await supabase.rpc('get_my_sessions'); const { data, error } = await supabase.rpc('get_my_sessions');
if (error) { if (error) {
throw error;
}
setSessions((data as AuthSession[]) || []);
} catch (error) {
logger.error('Failed to fetch sessions', { logger.error('Failed to fetch sessions', {
userId: user.id, userId: user.id,
action: 'fetch_sessions', action: 'fetch_sessions',
error: error.message error: error instanceof Error ? error.message : String(error)
}); });
handleError(error, { action: 'Load sessions', userId: user.id }); handleError(error, {
} else { action: 'Load active sessions',
setSessions((data as AuthSession[]) || []); userId: user.id
} });
setSessions([]);
} finally {
setLoadingSessions(false); setLoadingSessions(false);
}
}; };
const initiateSessionRevoke = async (sessionId: string) => { const initiateSessionRevoke = async (sessionId: string) => {

View File

@@ -18,17 +18,18 @@ import { z } from 'zod';
/** /**
* Schema for privacy settings in user_preferences * Schema for privacy settings in user_preferences
* Uses defaults for backward compatibility with incomplete data
*/ */
export const privacySettingsSchema = z.object({ export const privacySettingsSchema = z.object({
activity_visibility: z.enum(['public', 'private'] as const), activity_visibility: z.enum(['public', 'private'] as const).default('public'),
search_visibility: z.boolean(), search_visibility: z.boolean().default(true),
show_location: z.boolean(), show_location: z.boolean().default(false),
show_age: z.boolean(), show_age: z.boolean().default(false),
show_avatar: z.boolean(), show_avatar: z.boolean().default(true),
show_bio: z.boolean(), show_bio: z.boolean().default(true),
show_activity_stats: z.boolean(), show_activity_stats: z.boolean().default(true),
show_home_park: z.boolean() show_home_park: z.boolean().default(false)
}); }).passthrough();
/** /**
* Schema for profile privacy settings * Schema for profile privacy settings

View File

@@ -84,7 +84,7 @@ export interface AuthSession {
user_agent: string | null; user_agent: string | null;
ip: string | null; // Pre-hashed by database function ip: string | null; // Pre-hashed by database function
not_after: string | null; not_after: string | null;
aal: AALLevel | null; aal: string | null; // Changed to string to match get_my_sessions() return type
} }
/** /**

View File

@@ -0,0 +1,16 @@
-- Backfill missing privacy settings fields for existing users
UPDATE user_preferences
SET privacy_settings = privacy_settings || jsonb_build_object(
'show_avatar', COALESCE((privacy_settings->>'show_avatar')::boolean, true),
'show_bio', COALESCE((privacy_settings->>'show_bio')::boolean, true),
'show_activity_stats', COALESCE((privacy_settings->>'show_activity_stats')::boolean, true),
'show_home_park', COALESCE((privacy_settings->>'show_home_park')::boolean, false)
)
WHERE
privacy_settings IS NOT NULL
AND (
privacy_settings->>'show_avatar' IS NULL OR
privacy_settings->>'show_bio' IS NULL OR
privacy_settings->>'show_activity_stats' IS NULL OR
privacy_settings->>'show_home_park' IS NULL
);