feat: add platform selection frontend for platform admins
Frontend implementation of platform admin flow: - Update login.js to check for platform selection after login - Add platform selection page (/admin/select-platform) - Add platform context indicator in admin header - Add is_super_admin to UserResponse schema - Show "Super Admin" badge or platform name with switch option Platform admins now: 1. Login normally at /admin/login 2. Get redirected to /admin/select-platform if they have multiple platforms 3. See current platform in header with option to switch Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -166,7 +166,8 @@ function adminLogin() {
|
||||
loginLog.debug('User data stored:', {
|
||||
username: response.user.username,
|
||||
role: response.user.role,
|
||||
id: response.user.id
|
||||
id: response.user.id,
|
||||
is_super_admin: response.user.is_super_admin
|
||||
});
|
||||
}
|
||||
|
||||
@@ -180,12 +181,32 @@ function adminLogin() {
|
||||
});
|
||||
|
||||
// Show success message
|
||||
this.success = 'Login successful! Redirecting...';
|
||||
this.success = 'Login successful! Checking platform access...';
|
||||
loginLog.info('Success message displayed to user');
|
||||
|
||||
// Check if platform selection is required
|
||||
try {
|
||||
loginLog.info('Checking accessible platforms...');
|
||||
const platformsResponse = await apiClient.get('/api/v1/admin/auth/accessible-platforms');
|
||||
loginLog.debug('Accessible platforms response:', platformsResponse);
|
||||
|
||||
if (platformsResponse.requires_platform_selection) {
|
||||
// Platform admin needs to select a platform
|
||||
loginLog.info('Platform selection required, redirecting...');
|
||||
this.success = 'Login successful! Please select a platform...';
|
||||
window.location.href = '/admin/select-platform';
|
||||
return;
|
||||
}
|
||||
} catch (platformError) {
|
||||
loginLog.warn('Could not check platforms, proceeding to dashboard:', platformError);
|
||||
}
|
||||
|
||||
// Super admin or single platform - proceed to dashboard
|
||||
this.success = 'Login successful! Redirecting...';
|
||||
|
||||
// Check for last visited page (saved before logout)
|
||||
const lastPage = localStorage.getItem('admin_last_visited_page');
|
||||
const redirectTo = (lastPage && lastPage.startsWith('/admin/') && !lastPage.includes('/login'))
|
||||
const redirectTo = (lastPage && lastPage.startsWith('/admin/') && !lastPage.includes('/login') && !lastPage.includes('/select-platform'))
|
||||
? lastPage
|
||||
: '/admin/dashboard';
|
||||
|
||||
|
||||
156
static/admin/js/select-platform.js
Normal file
156
static/admin/js/select-platform.js
Normal file
@@ -0,0 +1,156 @@
|
||||
// static/admin/js/select-platform.js
|
||||
// Platform selection page for platform admins
|
||||
|
||||
const platformLog = window.LogConfig ? window.LogConfig.createLogger('PLATFORM_SELECT') : console;
|
||||
|
||||
function selectPlatform() {
|
||||
return {
|
||||
dark: false,
|
||||
loading: true,
|
||||
selecting: false,
|
||||
error: null,
|
||||
platforms: [],
|
||||
isSuperAdmin: false,
|
||||
|
||||
async init() {
|
||||
platformLog.info('=== PLATFORM SELECTION PAGE INITIALIZING ===');
|
||||
|
||||
// Set theme
|
||||
this.dark = localStorage.getItem('theme') === 'dark';
|
||||
|
||||
// Check if user is logged in
|
||||
const token = localStorage.getItem('admin_token');
|
||||
if (!token) {
|
||||
platformLog.warn('No token found, redirecting to login');
|
||||
window.location.href = '/admin/login';
|
||||
return;
|
||||
}
|
||||
|
||||
// Load accessible platforms
|
||||
await this.loadPlatforms();
|
||||
},
|
||||
|
||||
async loadPlatforms() {
|
||||
this.loading = true;
|
||||
this.error = null;
|
||||
|
||||
try {
|
||||
platformLog.info('Fetching accessible platforms...');
|
||||
const response = await apiClient.get('/api/v1/admin/auth/accessible-platforms');
|
||||
platformLog.debug('Platforms response:', response);
|
||||
|
||||
this.isSuperAdmin = response.is_super_admin;
|
||||
this.platforms = response.platforms || [];
|
||||
|
||||
if (this.isSuperAdmin) {
|
||||
platformLog.info('User is super admin, redirecting to dashboard...');
|
||||
setTimeout(() => {
|
||||
window.location.href = '/admin/dashboard';
|
||||
}, 1500);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!response.requires_platform_selection && this.platforms.length === 1) {
|
||||
// Only one platform assigned, auto-select it
|
||||
platformLog.info('Single platform assigned, auto-selecting...');
|
||||
await this.selectPlatform(this.platforms[0]);
|
||||
return;
|
||||
}
|
||||
|
||||
platformLog.info(`Loaded ${this.platforms.length} platforms`);
|
||||
|
||||
} catch (error) {
|
||||
platformLog.error('Failed to load platforms:', error);
|
||||
|
||||
if (error.message && error.message.includes('401')) {
|
||||
// Token expired or invalid
|
||||
window.location.href = '/admin/login';
|
||||
return;
|
||||
}
|
||||
|
||||
this.error = error.message || 'Failed to load platforms. Please try again.';
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
},
|
||||
|
||||
async selectPlatform(platform) {
|
||||
if (this.selecting) return;
|
||||
|
||||
this.selecting = true;
|
||||
this.error = null;
|
||||
platformLog.info(`Selecting platform: ${platform.code}`);
|
||||
|
||||
try {
|
||||
const response = await apiClient.post(
|
||||
`/api/v1/admin/auth/select-platform?platform_id=${platform.id}`
|
||||
);
|
||||
|
||||
platformLog.debug('Platform selection response:', response);
|
||||
|
||||
if (response.access_token) {
|
||||
// Store new token with platform context
|
||||
localStorage.setItem('admin_token', response.access_token);
|
||||
localStorage.setItem('token', response.access_token);
|
||||
|
||||
// Store selected platform info
|
||||
localStorage.setItem('admin_platform', JSON.stringify({
|
||||
id: platform.id,
|
||||
code: platform.code,
|
||||
name: platform.name
|
||||
}));
|
||||
|
||||
// Update user data if provided
|
||||
if (response.user) {
|
||||
localStorage.setItem('admin_user', JSON.stringify(response.user));
|
||||
}
|
||||
|
||||
platformLog.info('Platform selected successfully, redirecting to dashboard...');
|
||||
|
||||
// Redirect to dashboard or last visited page
|
||||
const lastPage = localStorage.getItem('admin_last_visited_page');
|
||||
const redirectTo = (lastPage && lastPage.startsWith('/admin/') && !lastPage.includes('/login') && !lastPage.includes('/select-platform'))
|
||||
? lastPage
|
||||
: '/admin/dashboard';
|
||||
|
||||
window.location.href = redirectTo;
|
||||
} else {
|
||||
throw new Error('No token received from server');
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
platformLog.error('Platform selection failed:', error);
|
||||
this.error = error.message || 'Failed to select platform. Please try again.';
|
||||
this.selecting = false;
|
||||
}
|
||||
},
|
||||
|
||||
logout() {
|
||||
platformLog.info('Logging out...');
|
||||
|
||||
fetch('/api/v1/admin/auth/logout', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${localStorage.getItem('admin_token')}`
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
platformLog.error('Logout API error:', error);
|
||||
})
|
||||
.finally(() => {
|
||||
localStorage.removeItem('admin_token');
|
||||
localStorage.removeItem('admin_user');
|
||||
localStorage.removeItem('admin_platform');
|
||||
localStorage.removeItem('token');
|
||||
window.location.href = '/admin/login';
|
||||
});
|
||||
},
|
||||
|
||||
toggleDarkMode() {
|
||||
this.dark = !this.dark;
|
||||
localStorage.setItem('theme', this.dark ? 'dark' : 'light');
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
platformLog.info('Platform selection module loaded');
|
||||
Reference in New Issue
Block a user