fix: resolve TypeScript TS18047 profile null type error in auth middleware
This commit is contained in:
+12
-6
@@ -20,13 +20,15 @@ export const requireAuth = async (req: AuthenticatedRequest, res: Response, next
|
|||||||
}
|
}
|
||||||
|
|
||||||
// P0-2: Lisans Kontrolü
|
// P0-2: Lisans Kontrolü
|
||||||
let { data: profile } = await supabase
|
const { data: profile } = await supabase
|
||||||
.from('profiles')
|
.from('profiles')
|
||||||
.select('license_status, trial_end_date')
|
.select('license_status, trial_end_date')
|
||||||
.eq('id', user.id)
|
.eq('id', user.id)
|
||||||
.maybeSingle();
|
.maybeSingle();
|
||||||
|
|
||||||
if (!profile) {
|
let userProfile = profile;
|
||||||
|
|
||||||
|
if (!userProfile) {
|
||||||
// Profil bulunamadıysa kullanıcıya otomatik deneme profili aç
|
// Profil bulunamadıysa kullanıcıya otomatik deneme profili aç
|
||||||
const trialEndDate = new Date();
|
const trialEndDate = new Date();
|
||||||
trialEndDate.setDate(trialEndDate.getDate() + 14);
|
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() }])
|
.insert([{ id: user.id, license_status: 'trial', trial_end_date: trialEndDate.toISOString() }])
|
||||||
.select()
|
.select()
|
||||||
.maybeSingle();
|
.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
|
// Deneme süresi dolmuş mu anlık kontrol et
|
||||||
if (profile.license_status === 'trial' && profile.trial_end_date) {
|
if (userProfile.license_status === 'trial' && userProfile.trial_end_date) {
|
||||||
const trialEnd = new Date(profile.trial_end_date);
|
const trialEnd = new Date(userProfile.trial_end_date);
|
||||||
if (trialEnd < new Date()) {
|
if (trialEnd < new Date()) {
|
||||||
await supabase
|
await supabase
|
||||||
.from('profiles')
|
.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' });
|
return res.status(403).json({ error: 'Forbidden: License is not active or in trial', code: 'LICENSE_INACTIVE' });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user