updated response to login API
This commit is contained in:
@@ -72,16 +72,36 @@ def validate_management_api_key(api_key):
|
|||||||
|
|
||||||
@admin_api.route('/login', methods=['POST'])
|
@admin_api.route('/login', methods=['POST'])
|
||||||
def admin_login():
|
def admin_login():
|
||||||
data = request.get_json()
|
try:
|
||||||
if not data or 'email' not in data or 'password' not in data:
|
data = request.get_json()
|
||||||
return jsonify({'message': 'Email and password are required'}), 400
|
if not data or 'email' not in data or 'password' not in data:
|
||||||
user = User.query.filter_by(email=data['email']).first()
|
return jsonify({
|
||||||
if not user or not user.is_admin or not user.check_password(data['password']):
|
'message': 'Email and password are required',
|
||||||
return jsonify({'message': 'Invalid credentials or not an admin'}), 401
|
'status': 'error'
|
||||||
token = jwt.encode({
|
}), 400
|
||||||
'user_id': user.id
|
|
||||||
}, current_app.config['SECRET_KEY'], algorithm="HS256")
|
user = User.query.filter_by(email=data['email']).first()
|
||||||
return jsonify({'token': token})
|
if not user or not user.is_admin or not user.check_password(data['password']):
|
||||||
|
return jsonify({
|
||||||
|
'message': 'Invalid credentials or not an admin',
|
||||||
|
'status': 'error'
|
||||||
|
}), 401
|
||||||
|
|
||||||
|
token = jwt.encode({
|
||||||
|
'user_id': user.id
|
||||||
|
}, current_app.config['SECRET_KEY'], algorithm="HS256")
|
||||||
|
|
||||||
|
return jsonify({
|
||||||
|
'token': token,
|
||||||
|
'status': 'success'
|
||||||
|
}), 200
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
current_app.logger.error(f"Login error: {str(e)}")
|
||||||
|
return jsonify({
|
||||||
|
'message': 'An error occurred during login',
|
||||||
|
'status': 'error'
|
||||||
|
}), 500
|
||||||
|
|
||||||
@admin_api.route('/management-token', methods=['POST'])
|
@admin_api.route('/management-token', methods=['POST'])
|
||||||
def get_management_token():
|
def get_management_token():
|
||||||
|
|||||||
@@ -473,6 +473,8 @@ async function authenticateInstance() {
|
|||||||
const password = formData.get('password');
|
const password = formData.get('password');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
console.log('Attempting login to:', `${instanceUrl}/api/admin/login`);
|
||||||
|
|
||||||
// First login to get token
|
// First login to get token
|
||||||
const loginResponse = await fetch(`${instanceUrl}/api/admin/login`, {
|
const loginResponse = await fetch(`${instanceUrl}/api/admin/login`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@@ -483,12 +485,21 @@ async function authenticateInstance() {
|
|||||||
body: JSON.stringify({ email, password })
|
body: JSON.stringify({ email, password })
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const responseData = await loginResponse.json();
|
||||||
|
console.log('Login response:', responseData);
|
||||||
|
|
||||||
if (!loginResponse.ok) {
|
if (!loginResponse.ok) {
|
||||||
const errorData = await loginResponse.json().catch(() => ({}));
|
throw new Error(responseData.message || 'Login failed');
|
||||||
throw new Error(errorData.message || 'Login failed');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const { token } = await loginResponse.json();
|
if (responseData.status !== 'success') {
|
||||||
|
throw new Error(responseData.message || 'Login failed');
|
||||||
|
}
|
||||||
|
|
||||||
|
const token = responseData.token;
|
||||||
|
if (!token) {
|
||||||
|
throw new Error('No token received from server');
|
||||||
|
}
|
||||||
|
|
||||||
// Then create management API key
|
// Then create management API key
|
||||||
const keyResponse = await fetch(`${instanceUrl}/api/admin/management-api-key`, {
|
const keyResponse = await fetch(`${instanceUrl}/api/admin/management-api-key`, {
|
||||||
@@ -503,12 +514,17 @@ async function authenticateInstance() {
|
|||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const keyData = await keyResponse.json();
|
||||||
|
console.log('API key response:', keyData);
|
||||||
|
|
||||||
if (!keyResponse.ok) {
|
if (!keyResponse.ok) {
|
||||||
const errorData = await keyResponse.json().catch(() => ({}));
|
throw new Error(keyData.message || 'Failed to create API key');
|
||||||
throw new Error(errorData.message || 'Failed to create API key');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const { api_key } = await keyResponse.json();
|
const api_key = keyData.api_key;
|
||||||
|
if (!api_key) {
|
||||||
|
throw new Error('No API key received from server');
|
||||||
|
}
|
||||||
|
|
||||||
// Save the token to our database
|
// Save the token to our database
|
||||||
const saveResponse = await fetch(`/instances/${instanceId}/save-token`, {
|
const saveResponse = await fetch(`/instances/${instanceId}/save-token`, {
|
||||||
@@ -520,9 +536,11 @@ async function authenticateInstance() {
|
|||||||
body: JSON.stringify({ token: api_key })
|
body: JSON.stringify({ token: api_key })
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const saveData = await saveResponse.json();
|
||||||
|
console.log('Save token response:', saveData);
|
||||||
|
|
||||||
if (!saveResponse.ok) {
|
if (!saveResponse.ok) {
|
||||||
const errorData = await saveResponse.json().catch(() => ({}));
|
throw new Error(saveData.message || 'Failed to save token');
|
||||||
throw new Error(errorData.message || 'Failed to save token');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show success and refresh
|
// Show success and refresh
|
||||||
|
|||||||
Reference in New Issue
Block a user