Skip to content

Commit a743acf

Browse files
feat: return JSON analytics and restrict to admin user
Co-authored-by: aider (deepseek/deepseek-chat) <aider@aider.chat>
1 parent c57bb6c commit a743acf

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

app/Http/Controllers/ReferralAnalyticsController.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,29 @@
33
namespace App\Http\Controllers;
44

55
use App\Models\ReferralClick;
6+
use Illuminate\Http\JsonResponse;
67
use Illuminate\Http\Request;
7-
use Illuminate\View\View;
88

99
class ReferralAnalyticsController extends Controller
1010
{
11-
public function index(): View
11+
public function index(): JsonResponse
1212
{
13+
// Ensure user is authenticated
14+
if (!auth()->check()) {
15+
return response()->json(['error' => 'Unauthenticated.'], 401);
16+
}
17+
18+
// Ensure user is the admin
19+
$adminUserId = (int) env('ADMIN_USER_ID', 0);
20+
if (auth()->id() !== $adminUserId) {
21+
return response()->json(['error' => 'Forbidden.'], 403);
22+
}
23+
1324
$clicks = ReferralClick::selectRaw('referrer, COUNT(*) as total')
1425
->groupBy('referrer')
1526
->orderByDesc('total')
1627
->get();
1728

18-
return view('referral.analytics', compact('clicks'));
29+
return response()->json($clicks);
1930
}
2031
}

0 commit comments

Comments
 (0)