Skip to content

Commit 89d9491

Browse files
committed
fix: error in keyword
1 parent e125c81 commit 89d9491

7 files changed

Lines changed: 1251 additions & 5 deletions

File tree

KEYWORD_API_AUTH_FIX.md

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# Keyword API Tester - Authentication Fix
2+
3+
## Issue
4+
The Keyword API Tester was returning **401 Unauthorized** errors when trying to make API calls.
5+
6+
## Root Cause
7+
The keyword API endpoints were configured in `routes/api.php` inside the `auth:sanctum` middleware group. Sanctum authentication requires Bearer tokens, which are meant for external API access (like WordPress plugin integration), not for web interface usage.
8+
9+
## Solution
10+
11+
### 1. Created Web API Routes
12+
Added dedicated web routes in `routes/web.php` for the keyword API that use Laravel's standard web session authentication:
13+
14+
```php
15+
// Keyword API endpoints for web interface
16+
Route::prefix('web-api/keywords')->name('web-api.keywords.')->group(function () {
17+
Route::post('suggestions', [\App\Http\Controllers\Api\KeywordController::class, 'getSuggestions'])->name('suggestions');
18+
Route::post('related', [\App\Http\Controllers\Api\KeywordController::class, 'getRelatedKeywords'])->name('related');
19+
Route::post('analyze', [\App\Http\Controllers\Api\KeywordController::class, 'analyzeKeyword'])->name('analyze');
20+
Route::post('batch-analyze', [\App\Http\Controllers\Api\KeywordController::class, 'batchAnalyze'])->name('batch-analyze');
21+
Route::post('clear-cache', [\App\Http\Controllers\Api\KeywordController::class, 'clearCache'])->name('clear-cache');
22+
});
23+
```
24+
25+
These routes are inside the `auth:web` middleware group (inherited from the parent group), so they:
26+
- ✅ Work with Laravel session authentication
27+
- ✅ Include CSRF protection
28+
- ✅ Are accessible from the web interface
29+
- ✅ Use the same controller methods as the API routes
30+
31+
### 2. Updated Frontend to Use Web Routes
32+
Updated all API calls in `resources/js/pages/keyword-tester.tsx` to use the new web API endpoints:
33+
34+
**Before:**
35+
```typescript
36+
axios.post('/api/keywords/suggestions', { ... })
37+
axios.post('/api/keywords/related', { ... })
38+
axios.post('/api/keywords/analyze', { ... })
39+
axios.post('/api/keywords/batch-analyze', { ... })
40+
```
41+
42+
**After:**
43+
```typescript
44+
axios.post('/web-api/keywords/suggestions', { ... })
45+
axios.post('/web-api/keywords/related', { ... })
46+
axios.post('/web-api/keywords/analyze', { ... })
47+
axios.post('/web-api/keywords/batch-analyze', { ... })
48+
```
49+
50+
### 3. Added CSRF Token Configuration
51+
Added automatic CSRF token configuration in the keyword-tester component:
52+
53+
```typescript
54+
// Get CSRF token from meta tag
55+
useEffect(() => {
56+
const token = document.querySelector('meta[name="csrf-token"]')?.getAttribute('content');
57+
if (token) {
58+
axios.defaults.headers.common['X-CSRF-TOKEN'] = token;
59+
}
60+
}, []);
61+
```
62+
63+
## API Architecture
64+
65+
Now we have two sets of keyword API endpoints:
66+
67+
### 1. Web API Endpoints (Session Auth)
68+
**Base URL:** `/web-api/keywords/`
69+
**Authentication:** Laravel session (web middleware)
70+
**Used by:** Dashboard/Web Interface
71+
**CSRF:** Required
72+
73+
Endpoints:
74+
- `POST /web-api/keywords/suggestions`
75+
- `POST /web-api/keywords/related`
76+
- `POST /web-api/keywords/analyze`
77+
- `POST /web-api/keywords/batch-analyze`
78+
- `POST /web-api/keywords/clear-cache`
79+
80+
### 2. API Endpoints (Sanctum Auth)
81+
**Base URL:** `/api/keywords/`
82+
**Authentication:** Bearer token (Sanctum)
83+
**Used by:** External clients, WordPress plugin
84+
**CSRF:** Not required
85+
86+
Endpoints:
87+
- `POST /api/keywords/suggestions`
88+
- `POST /api/keywords/related`
89+
- `POST /api/keywords/analyze`
90+
- `POST /api/keywords/batch-analyze`
91+
- `POST /api/keywords/clear-cache`
92+
93+
### 3. WordPress Plugin Endpoints (API Key Auth)
94+
**Base URL:** `/api/ai/keywords/`
95+
**Authentication:** API Key in X-API-KEY header
96+
**Used by:** WordPress plugin
97+
**CSRF:** Not required
98+
99+
Endpoints:
100+
- `POST /api/ai/keywords/suggestions`
101+
- `POST /api/ai/keywords/related`
102+
- `POST /api/ai/keywords/analyze`
103+
- `POST /api/ai/keywords/batch-analyze`
104+
105+
## How It Works Now
106+
107+
1. User logs into the dashboard (Laravel session created)
108+
2. User navigates to **Keyword Tester** page
109+
3. Page loads with CSRF token from meta tag
110+
4. User enters a keyword and clicks "Get Suggestions"
111+
5. Frontend makes POST request to `/web-api/keywords/suggestions`
112+
6. Laravel validates:
113+
- ✅ User is authenticated (session)
114+
- ✅ CSRF token is valid
115+
7. Controller processes the request
116+
8. Results are returned and displayed
117+
118+
## Testing
119+
120+
To test the fix:
121+
122+
1. **Login to the dashboard**
123+
```
124+
Visit: https://wegenius.test/login
125+
```
126+
127+
2. **Navigate to Keyword Tester**
128+
```
129+
Click "Keyword Tester" in the sidebar
130+
OR visit: https://wegenius.test/keyword-tester
131+
```
132+
133+
3. **Test the API**
134+
- Enter a keyword (e.g., "android")
135+
- Click "Get Suggestions"
136+
- You should see results without any authentication errors
137+
138+
## Files Modified
139+
140+
1. **routes/web.php**
141+
- Added web API routes for keyword endpoints
142+
143+
2. **resources/js/pages/keyword-tester.tsx**
144+
- Updated all API calls to use `/web-api/keywords/` instead of `/api/keywords/`
145+
- Added CSRF token configuration with useEffect
146+
147+
## Status
148+
149+
**FIXED** - The authentication issue is resolved. The Keyword API Tester now works correctly from the web interface!
150+
151+
---
152+
153+
**Date:** October 11, 2025
154+
**Status:** Production Ready

0 commit comments

Comments
 (0)