From 326bd1bd724f61a9d76a1f3585218e112a2ef0a3 Mon Sep 17 00:00:00 2001 From: Kobe Date: Mon, 9 Jun 2025 22:34:19 +0200 Subject: [PATCH] Added JWT token process --- routes/admin_api.py | 4 ++- templates/main/instances.html | 48 ++++++++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/routes/admin_api.py b/routes/admin_api.py index df3a08b..aa5620c 100644 --- a/routes/admin_api.py +++ b/routes/admin_api.py @@ -88,7 +88,9 @@ def admin_login(): }), 401 token = jwt.encode({ - 'user_id': user.id + 'user_id': user.id, + 'is_admin': True, + 'exp': datetime.utcnow() + timedelta(days=1) # Token expires in 1 day }, current_app.config['SECRET_KEY'], algorithm="HS256") return jsonify({ diff --git a/templates/main/instances.html b/templates/main/instances.html index 9e8d622..a318fcf 100644 --- a/templates/main/instances.html +++ b/templates/main/instances.html @@ -485,8 +485,23 @@ async function authenticateInstance() { body: JSON.stringify({ email, password }) }); - const responseData = await loginResponse.json(); - console.log('Login response:', responseData); + // Check content type + const contentType = loginResponse.headers.get('content-type'); + if (!contentType || !contentType.includes('application/json')) { + console.error('Unexpected content type:', contentType); + const text = await loginResponse.text(); + console.error('Response text:', text); + throw new Error(`Server returned non-JSON response (${contentType}). Please check if the instance is properly configured.`); + } + + let responseData; + try { + responseData = await loginResponse.json(); + console.log('Login response:', responseData); + } catch (e) { + console.error('Failed to parse JSON response:', e); + throw new Error('Invalid JSON response from server'); + } if (!loginResponse.ok) { throw new Error(responseData.message || 'Login failed'); @@ -514,8 +529,23 @@ async function authenticateInstance() { }) }); - const keyData = await keyResponse.json(); - console.log('API key response:', keyData); + // Check content type for key response + const keyContentType = keyResponse.headers.get('content-type'); + if (!keyContentType || !keyContentType.includes('application/json')) { + console.error('Unexpected content type for key response:', keyContentType); + const text = await keyResponse.text(); + console.error('Key response text:', text); + throw new Error(`Server returned non-JSON response for API key (${keyContentType})`); + } + + let keyData; + try { + keyData = await keyResponse.json(); + console.log('API key response:', keyData); + } catch (e) { + console.error('Failed to parse JSON response for API key:', e); + throw new Error('Invalid JSON response from server for API key'); + } if (!keyResponse.ok) { throw new Error(keyData.message || 'Failed to create API key'); @@ -536,8 +566,14 @@ async function authenticateInstance() { body: JSON.stringify({ token: api_key }) }); - const saveData = await saveResponse.json(); - console.log('Save token response:', saveData); + let saveData; + try { + saveData = await saveResponse.json(); + console.log('Save token response:', saveData); + } catch (e) { + console.error('Failed to parse JSON response for save token:', e); + throw new Error('Invalid JSON response from server for save token'); + } if (!saveResponse.ok) { throw new Error(saveData.message || 'Failed to save token');