import { Request, Response } from 'express'; import { supabase } from '../lib/supabase'; // POST /api/auth/register // E-posta + şifre ile yeni kullanıcı kaydı. // device_id: Electron'dan gönderilen cihaz kimliği (opsiyonel, sadece Electron build'de gelir). // Aynı device_id ile daha önce trial başlatılmışsa kayıt reddedilir. export const register = async (req: Request, res: Response) => { const { email, password, device_id } = req.body; if (!email || !password) { return res.status(400).json({ error: 'E-posta ve şifre gereklidir.' }); } if (password.length < 8) { return res.status(400).json({ error: 'Şifre en az 8 karakter olmalıdır.' }); } // Device ID varsa aynı cihazdan daha önce trial açılmış mı kontrol et if (device_id) { const { data: existingDevice } = await supabase .from('profiles') .select('id, license_status') .eq('trial_device_id', device_id) .maybeSingle(); if (existingDevice) { return res.status(409).json({ error: 'Bu cihazda daha önce deneme sürümü kullanılmıştır. Hesabınıza giriş yapın veya bir lisans anahtarı edinin.', code: 'DEVICE_TRIAL_USED', }); } } // Supabase Auth'ta kullanıcı oluştur (service role ile — email doğrulama zorunluluğu bypass) const { data, error } = await supabase.auth.admin.createUser({ email, password, email_confirm: true, // E-posta onayı gerektirmeden direkt aktif et }); if (error) { // Supabase hata mesajlarını Türkçeye çevir if (error.message?.toLowerCase().includes('already registered') || error.message?.includes('already been registered')) { return res.status(409).json({ error: 'Bu e-posta adresi zaten kayıtlı.' }); } console.error('[auth] register error:', error.message); return res.status(400).json({ error: 'Kayıt oluşturulamadı: ' + error.message }); } const userId = data.user?.id; if (!userId) { return res.status(500).json({ error: 'Kullanıcı oluşturuldu fakat ID alınamadı.' }); } // Device ID'yi profile'a kaydet (Supabase trigger zaten trial kolonlarını oluşturdu) if (device_id) { await supabase .from('profiles') .update({ trial_device_id: device_id }) .eq('id', userId); } // Kayıt başarılı — kullanıcı artık Supabase Auth üzerinden giriş yapabilir return res.status(201).json({ success: true, message: '3 günlük deneme sürümünüz başlatıldı.', userId, }); }; // POST /api/auth/check-device // Bir device_id'nin daha önce trial için kullanılıp kullanılmadığını kontrol eder. // Kayıt formunda "Kayıt Ol" butonuna basmadan önce çağrılır. export const checkDevice = async (req: Request, res: Response) => { const { device_id } = req.body; if (!device_id) { return res.status(400).json({ error: 'device_id gerekli.' }); } const { data } = await supabase .from('profiles') .select('id') .eq('trial_device_id', device_id) .maybeSingle(); return res.json({ deviceUsed: !!data }); };