mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-22 07:31:12 -05:00
Fix edge function console statements
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import "jsr:@supabase/functions-js/edge-runtime.d.ts";
|
||||
import { createClient } from "https://esm.sh/@supabase/supabase-js@2.57.4";
|
||||
import { startRequest, endRequest } from '../_shared/logger.ts';
|
||||
import { edgeLogger, startRequest, endRequest } from '../_shared/logger.ts';
|
||||
|
||||
const corsHeaders = {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
@@ -12,11 +12,14 @@ const CLOUDFLARE_API_TOKEN = Deno.env.get('CLOUDFLARE_IMAGES_API_TOKEN');
|
||||
|
||||
// Validate configuration at startup
|
||||
if (!CLOUDFLARE_ACCOUNT_ID || !CLOUDFLARE_API_TOKEN) {
|
||||
console.error('[OAuth Profile] Missing Cloudflare configuration:', {
|
||||
edgeLogger.error('Missing Cloudflare configuration', {
|
||||
action: 'oauth_profile_init',
|
||||
hasAccountId: !!CLOUDFLARE_ACCOUNT_ID,
|
||||
hasApiToken: !!CLOUDFLARE_API_TOKEN,
|
||||
});
|
||||
console.error('[OAuth Profile] Please configure CLOUDFLARE_ACCOUNT_ID and CLOUDFLARE_IMAGES_API_TOKEN in Supabase Edge Function secrets');
|
||||
edgeLogger.error('Please configure CLOUDFLARE_ACCOUNT_ID and CLOUDFLARE_IMAGES_API_TOKEN in Supabase Edge Function secrets', {
|
||||
action: 'oauth_profile_init'
|
||||
});
|
||||
}
|
||||
|
||||
interface GoogleUserMetadata {
|
||||
@@ -96,14 +99,14 @@ Deno.serve(async (req) => {
|
||||
const { data: { user }, error: authError } = await supabase.auth.getUser(token);
|
||||
|
||||
if (authError || !user) {
|
||||
console.error('[OAuth Profile] Authentication failed:', authError);
|
||||
edgeLogger.error('Authentication failed', { action: 'oauth_profile', error: authError, requestId: tracking.requestId });
|
||||
return new Response(JSON.stringify({ error: 'Unauthorized' }), {
|
||||
status: 401,
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||
});
|
||||
}
|
||||
|
||||
console.log('[OAuth Profile] Processing profile for user:', user.id);
|
||||
edgeLogger.info('Processing profile for user', { action: 'oauth_profile', userId: user.id, requestId: tracking.requestId });
|
||||
|
||||
// CRITICAL: Check ban status immediately
|
||||
const { data: banProfile } = await supabase
|
||||
@@ -118,7 +121,8 @@ Deno.serve(async (req) => {
|
||||
? `Your account has been suspended. Reason: ${banProfile.ban_reason}`
|
||||
: 'Your account has been suspended. Contact support for assistance.';
|
||||
|
||||
console.log('[OAuth Profile] User is banned, rejecting authentication', {
|
||||
edgeLogger.info('User is banned, rejecting authentication', {
|
||||
action: 'oauth_profile_banned',
|
||||
requestId: tracking.requestId,
|
||||
duration,
|
||||
hasBanReason: !!banProfile.ban_reason
|
||||
@@ -144,7 +148,9 @@ Deno.serve(async (req) => {
|
||||
if (discordIdentity) {
|
||||
userMetadata = discordIdentity.identity_data || {};
|
||||
|
||||
console.log('[OAuth Profile] Discord identity_data:', {
|
||||
edgeLogger.info('Discord identity_data', {
|
||||
action: 'oauth_profile_discord',
|
||||
requestId: tracking.requestId,
|
||||
hasAvatarUrl: !!(userMetadata as DiscordUserMetadata).avatar_url,
|
||||
hasFullName: !!(userMetadata as DiscordUserMetadata).full_name,
|
||||
hasGlobalName: !!(userMetadata as DiscordUserMetadata).custom_claims?.global_name,
|
||||
@@ -152,7 +158,7 @@ Deno.serve(async (req) => {
|
||||
hasEmail: !!(userMetadata as DiscordUserMetadata).email
|
||||
});
|
||||
} else {
|
||||
console.warn('[OAuth Profile] Discord provider found but no Discord identity in user.identities');
|
||||
edgeLogger.warn('Discord provider found but no Discord identity in user.identities', { action: 'oauth_profile_discord', requestId: tracking.requestId });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,7 +172,7 @@ Deno.serve(async (req) => {
|
||||
avatarUrl = googleData.picture || null;
|
||||
displayName = googleData.name || null;
|
||||
usernameBase = googleData.email?.split('@')[0] || null;
|
||||
console.log('[OAuth Profile] Google user:', { avatarUrl, displayName, usernameBase });
|
||||
edgeLogger.info('Google user', { action: 'oauth_profile_google', requestId: tracking.requestId, avatarUrl, displayName, usernameBase });
|
||||
} else if (provider === 'discord') {
|
||||
const discordData = userMetadata as DiscordUserMetadata;
|
||||
|
||||
@@ -187,15 +193,17 @@ Deno.serve(async (req) => {
|
||||
|
||||
// Validation logging
|
||||
if (!discordId) {
|
||||
console.error('[OAuth Profile] Discord user ID missing from provider_id/sub - OAuth data incomplete');
|
||||
edgeLogger.error('Discord user ID missing from provider_id/sub - OAuth data incomplete', { action: 'oauth_profile_discord', requestId: tracking.requestId });
|
||||
}
|
||||
|
||||
if (!usernameBase) {
|
||||
console.warn('[OAuth Profile] Discord username missing - using ID as fallback');
|
||||
edgeLogger.warn('Discord username missing - using ID as fallback', { action: 'oauth_profile_discord', requestId: tracking.requestId });
|
||||
usernameBase = discordId;
|
||||
}
|
||||
|
||||
console.log('[OAuth Profile] Discord user (Supabase format):', {
|
||||
edgeLogger.info('Discord user (Supabase format)', {
|
||||
action: 'oauth_profile_discord',
|
||||
requestId: tracking.requestId,
|
||||
avatarUrl,
|
||||
displayName,
|
||||
usernameBase,
|
||||
@@ -205,7 +213,7 @@ Deno.serve(async (req) => {
|
||||
source: discordData.avatar_url ? 'avatar_url' : discordData.picture ? 'picture' : 'none'
|
||||
});
|
||||
} else {
|
||||
console.log('[OAuth Profile] Unsupported provider:', provider);
|
||||
edgeLogger.info('Unsupported provider', { action: 'oauth_profile', provider, requestId: tracking.requestId });
|
||||
return new Response(JSON.stringify({ success: true, message: 'Provider not supported' }), {
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||
});
|
||||
@@ -220,7 +228,7 @@ Deno.serve(async (req) => {
|
||||
|
||||
if (profile?.avatar_image_id) {
|
||||
const duration = endRequest(tracking);
|
||||
console.log('[OAuth Profile] Avatar already exists, skipping', { requestId: tracking.requestId, duration });
|
||||
edgeLogger.info('Avatar already exists, skipping', { action: 'oauth_profile', requestId: tracking.requestId, duration });
|
||||
return new Response(JSON.stringify({ success: true, message: 'Avatar already exists', requestId: tracking.requestId }), {
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json', 'X-Request-ID': tracking.requestId },
|
||||
});
|
||||
@@ -233,14 +241,19 @@ Deno.serve(async (req) => {
|
||||
if (avatarUrl) {
|
||||
// Validate secrets before attempting upload
|
||||
if (!CLOUDFLARE_ACCOUNT_ID || !CLOUDFLARE_API_TOKEN) {
|
||||
console.warn('[OAuth Profile] Cloudflare secrets not configured, skipping avatar upload');
|
||||
console.warn('[OAuth Profile] Missing:', {
|
||||
edgeLogger.warn('Cloudflare secrets not configured, skipping avatar upload', {
|
||||
action: 'oauth_profile_upload',
|
||||
requestId: tracking.requestId
|
||||
});
|
||||
edgeLogger.warn('Missing Cloudflare configuration', {
|
||||
action: 'oauth_profile_upload',
|
||||
requestId: tracking.requestId,
|
||||
accountId: !CLOUDFLARE_ACCOUNT_ID,
|
||||
apiToken: !CLOUDFLARE_API_TOKEN,
|
||||
});
|
||||
} else {
|
||||
try {
|
||||
console.log('[OAuth Profile] Downloading avatar from:', avatarUrl);
|
||||
edgeLogger.info('Downloading avatar', { action: 'oauth_profile_upload', avatarUrl, requestId: tracking.requestId });
|
||||
|
||||
// Download image with timeout
|
||||
const controller = new AbortController();
|
||||
@@ -262,7 +275,9 @@ Deno.serve(async (req) => {
|
||||
throw new Error('Image too large (max 10MB)');
|
||||
}
|
||||
|
||||
console.log('[OAuth Profile] Downloaded image:', {
|
||||
edgeLogger.info('Downloaded image', {
|
||||
action: 'oauth_profile_upload',
|
||||
requestId: tracking.requestId,
|
||||
size: imageBlob.size,
|
||||
type: imageBlob.type,
|
||||
});
|
||||
@@ -285,7 +300,7 @@ Deno.serve(async (req) => {
|
||||
const uploadData = await uploadUrlResponse.json();
|
||||
const uploadURL = uploadData.result.uploadURL;
|
||||
|
||||
console.log('[OAuth Profile] Got Cloudflare upload URL');
|
||||
edgeLogger.info('Got Cloudflare upload URL', { action: 'oauth_profile_upload', requestId: tracking.requestId });
|
||||
|
||||
// Upload to Cloudflare
|
||||
const formData = new FormData();
|
||||
@@ -305,12 +320,14 @@ Deno.serve(async (req) => {
|
||||
if (result.success) {
|
||||
cloudflareImageId = result.result.id;
|
||||
cloudflareImageUrl = `https://cdn.thrillwiki.com/images/${cloudflareImageId}/avatar`;
|
||||
console.log('[OAuth Profile] Uploaded to Cloudflare:', { cloudflareImageId, cloudflareImageUrl });
|
||||
edgeLogger.info('Uploaded to Cloudflare', { action: 'oauth_profile_upload', requestId: tracking.requestId, cloudflareImageId, cloudflareImageUrl });
|
||||
} else {
|
||||
throw new Error('Cloudflare upload failed');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[OAuth Profile] Avatar upload failed:', {
|
||||
edgeLogger.error('Avatar upload failed', {
|
||||
action: 'oauth_profile_upload',
|
||||
requestId: tracking.requestId,
|
||||
error: error.message,
|
||||
provider: provider,
|
||||
accountId: CLOUDFLARE_ACCOUNT_ID,
|
||||
@@ -338,7 +355,7 @@ Deno.serve(async (req) => {
|
||||
if (usernameBase && profile?.username?.startsWith('user_')) {
|
||||
const newUsername = await ensureUniqueUsername(supabase, usernameBase, user.id);
|
||||
updateData.username = newUsername;
|
||||
console.log('[OAuth Profile] Updating generic username from', profile.username, 'to', newUsername);
|
||||
edgeLogger.info('Updating generic username', { action: 'oauth_profile', requestId: tracking.requestId, oldUsername: profile.username, newUsername });
|
||||
}
|
||||
|
||||
// Only update if we have data to update
|
||||
@@ -349,18 +366,18 @@ Deno.serve(async (req) => {
|
||||
.eq('user_id', user.id);
|
||||
|
||||
if (updateError) {
|
||||
console.error('[OAuth Profile] Failed to update profile:', updateError);
|
||||
edgeLogger.error('Failed to update profile', { action: 'oauth_profile', requestId: tracking.requestId, error: updateError });
|
||||
return new Response(JSON.stringify({ error: 'Failed to update profile' }), {
|
||||
status: 500,
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
|
||||
});
|
||||
}
|
||||
|
||||
console.log('[OAuth Profile] Profile updated successfully', { requestId: tracking.requestId });
|
||||
edgeLogger.info('Profile updated successfully', { action: 'oauth_profile', requestId: tracking.requestId });
|
||||
}
|
||||
|
||||
const duration = endRequest(tracking);
|
||||
console.log('[OAuth Profile] Processing complete', { requestId: tracking.requestId, duration });
|
||||
edgeLogger.info('Processing complete', { action: 'oauth_profile', requestId: tracking.requestId, duration });
|
||||
|
||||
return new Response(JSON.stringify({
|
||||
success: true,
|
||||
@@ -373,7 +390,7 @@ Deno.serve(async (req) => {
|
||||
|
||||
} catch (error) {
|
||||
const duration = endRequest(tracking);
|
||||
console.error('[OAuth Profile] Error:', error, { requestId: tracking.requestId, duration });
|
||||
edgeLogger.error('Error in oauth profile processing', { action: 'oauth_profile', requestId: tracking.requestId, duration, error: error.message });
|
||||
return new Response(JSON.stringify({ error: error.message, requestId: tracking.requestId }), {
|
||||
status: 500,
|
||||
headers: { ...corsHeaders, 'Content-Type': 'application/json', 'X-Request-ID': tracking.requestId },
|
||||
|
||||
Reference in New Issue
Block a user