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ü
|
||||
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' });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user