fix: Use correct token key (studio_token) in admin pages
- Fix localStorage token key from 'token' to 'studio_token' - Add debug logging for token authentication - Add automatic redirect to login on 401 - Add error messages for missing/expired tokens Affected pages: - /admin/token-usage - /admin/users - /admin Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
@@ -39,7 +39,7 @@ export default function AdminDashboard() {
|
||||
cmsApi.getAllUsers(),
|
||||
fetch(`${process.env.NEXT_PUBLIC_CMS_API_URL || 'http://localhost:3001'}/admin/token-usage`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${localStorage.getItem('token')}`,
|
||||
'Authorization': `Bearer ${localStorage.getItem('studio_token')}`,
|
||||
},
|
||||
})
|
||||
]);
|
||||
|
||||
@@ -54,12 +54,32 @@ export default function AdminTokenTracking() {
|
||||
|
||||
const loadTokenUsage = async () => {
|
||||
try {
|
||||
const token = localStorage.getItem('studio_token');
|
||||
console.log('[TokenUsage] Token from localStorage:', token ? 'Present (studio_token)' : 'Missing');
|
||||
|
||||
if (!token) {
|
||||
console.error('[TokenUsage] No authentication token found!');
|
||||
alert('No authentication token found. Please login again.');
|
||||
window.location.href = '/auth/login';
|
||||
return;
|
||||
}
|
||||
|
||||
const response = await fetch(`${process.env.NEXT_PUBLIC_CMS_API_URL || 'http://localhost:3001'}/admin/token-usage`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${localStorage.getItem('token')}`,
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
console.log('[TokenUsage] API Response status:', response.status);
|
||||
|
||||
if (response.status === 401) {
|
||||
console.error('[TokenUsage] Unauthorized - Token may be expired');
|
||||
alert('Session expired. Please login again.');
|
||||
window.location.href = '/auth/login';
|
||||
return;
|
||||
}
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
setUsage(data.usage || []);
|
||||
@@ -73,7 +93,7 @@ export default function AdminTokenTracking() {
|
||||
`${process.env.NEXT_PUBLIC_CMS_API_URL || 'http://localhost:3001'}/admin/users/${user.user_id}/token-limit/check`,
|
||||
{
|
||||
headers: {
|
||||
'Authorization': `Bearer ${localStorage.getItem('token')}`,
|
||||
'Authorization': `Bearer ${localStorage.getItem('studio_token')}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
@@ -110,7 +130,7 @@ export default function AdminTokenTracking() {
|
||||
{
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${localStorage.getItem('token')}`,
|
||||
'Authorization': `Bearer ${localStorage.getItem('studio_token')}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
@@ -126,7 +146,7 @@ export default function AdminTokenTracking() {
|
||||
`${process.env.NEXT_PUBLIC_CMS_API_URL || 'http://localhost:3001'}/admin/users/${userId}/token-limit/check`,
|
||||
{
|
||||
headers: {
|
||||
'Authorization': `Bearer ${localStorage.getItem('token')}`,
|
||||
'Authorization': `Bearer ${localStorage.getItem('studio_token')}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
@@ -50,7 +50,7 @@ export default function UsersPage() {
|
||||
`${process.env.NEXT_PUBLIC_CMS_API_URL || 'http://localhost:3001'}/admin/users/${user.id}/token-limit/check`,
|
||||
{
|
||||
headers: {
|
||||
'Authorization': `Bearer ${localStorage.getItem('token')}`,
|
||||
'Authorization': `Bearer ${localStorage.getItem('studio_token')}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user