Added JWT token process

This commit is contained in:
2025-06-09 22:34:19 +02:00
parent 8f76832f69
commit 326bd1bd72
2 changed files with 45 additions and 7 deletions

View File

@@ -88,7 +88,9 @@ def admin_login():
}), 401 }), 401
token = jwt.encode({ 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") }, current_app.config['SECRET_KEY'], algorithm="HS256")
return jsonify({ return jsonify({

View File

@@ -485,8 +485,23 @@ async function authenticateInstance() {
body: JSON.stringify({ email, password }) body: JSON.stringify({ email, password })
}); });
const responseData = await loginResponse.json(); // 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); 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) { if (!loginResponse.ok) {
throw new Error(responseData.message || 'Login failed'); throw new Error(responseData.message || 'Login failed');
@@ -514,8 +529,23 @@ async function authenticateInstance() {
}) })
}); });
const keyData = await keyResponse.json(); // 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); 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) { if (!keyResponse.ok) {
throw new Error(keyData.message || 'Failed to create API key'); throw new Error(keyData.message || 'Failed to create API key');
@@ -536,8 +566,14 @@ async function authenticateInstance() {
body: JSON.stringify({ token: api_key }) body: JSON.stringify({ token: api_key })
}); });
const saveData = await saveResponse.json(); let saveData;
try {
saveData = await saveResponse.json();
console.log('Save token response:', saveData); 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) { if (!saveResponse.ok) {
throw new Error(saveData.message || 'Failed to save token'); throw new Error(saveData.message || 'Failed to save token');