From 2324cb78a6e7cc975642832ef5bb1c5951974639 Mon Sep 17 00:00:00 2001 From: Mustafa Yildiz Date: Sat, 15 Aug 2026 17:04:14 +0300 Subject: [PATCH] fix: resolve TypeScript TS18047 profile null type error in auth middleware --- src/middleware/auth.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/middleware/auth.ts b/src/middleware/auth.ts index 8e69f2c..1f367e2 100644 --- a/src/middleware/auth.ts +++ b/src/middleware/auth.ts @@ -20,13 +20,15 @@ export const requireAuth = async (req: AuthenticatedRequest, res: Response, next } // P0-2: Lisans Kontrolü - let { data: profile } = await supabase + const { data: profile } = await supabase .from('profiles') .select('license_status, trial_end_date') .eq('id', user.id) .maybeSingle(); - if (!profile) { + let userProfile = profile; + + if (!userProfile) { // Profil bulunamadıysa kullanıcıya otomatik deneme profili aç const trialEndDate = new Date(); trialEndDate.setDate(trialEndDate.getDate() + 14); @@ -35,12 +37,16 @@ export const requireAuth = async (req: AuthenticatedRequest, res: Response, next .insert([{ id: user.id, license_status: 'trial', trial_end_date: trialEndDate.toISOString() }]) .select() .maybeSingle(); - profile = newProfile || { license_status: 'trial' }; + userProfile = newProfile || { license_status: 'trial', trial_end_date: trialEndDate.toISOString() }; + } + + if (!userProfile) { + return res.status(403).json({ error: 'Forbidden: User profile not found' }); } // Deneme süresi dolmuş mu anlık kontrol et - if (profile.license_status === 'trial' && profile.trial_end_date) { - const trialEnd = new Date(profile.trial_end_date); + if (userProfile.license_status === 'trial' && userProfile.trial_end_date) { + const trialEnd = new Date(userProfile.trial_end_date); if (trialEnd < new Date()) { await supabase .from('profiles') @@ -54,7 +60,7 @@ export const requireAuth = async (req: AuthenticatedRequest, res: Response, next } } - if (profile.license_status !== 'active' && profile.license_status !== 'trial') { + if (userProfile.license_status !== 'active' && userProfile.license_status !== 'trial') { return res.status(403).json({ error: 'Forbidden: License is not active or in trial', code: 'LICENSE_INACTIVE' }); }