Files
orion/static/vendor/js/login.js
Samir Boulahtit d34021cfa2 fix: extend architecture validation to vendor/shared JS files
- Include static/vendor/js and static/shared/js in JS validation
- Fix onboarding.js: use apiClient (not window.apiClient), use logger
- Fix onboarding.js: use relative paths (not /api/v1/ prefix)
- Add noqa comments for standalone pages (login, onboarding)
- Add ...data() to messages.js for layout inheritance

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-27 22:16:30 +01:00

156 lines
5.8 KiB
JavaScript

// app/static/vendor/js/login.js
// noqa: js-003 - Standalone login page without vendor layout
// noqa: js-004 - Standalone page has no currentPage sidebar highlight
/**
* Vendor login page logic
*/
// Create custom logger for vendor login page
const vendorLoginLog = window.LogConfig.createLogger('VENDOR-LOGIN');
function vendorLogin() {
return {
credentials: {
username: '',
password: ''
},
vendor: null,
vendorCode: null,
loading: false,
checked: false,
error: '',
success: '',
errors: {},
dark: false,
async init() {
vendorLoginLog.info('=== VENDOR LOGIN PAGE INITIALIZING ===');
// Load theme
const theme = localStorage.getItem('theme');
if (theme === 'dark') {
this.dark = true;
}
vendorLoginLog.debug('Dark mode:', this.dark);
// Get vendor code from URL path
const pathSegments = window.location.pathname.split('/').filter(Boolean);
if (pathSegments[0] === 'vendor' && pathSegments[1]) {
this.vendorCode = pathSegments[1];
vendorLoginLog.debug('Vendor code from URL:', this.vendorCode);
await this.loadVendor();
}
this.checked = true;
vendorLoginLog.info('=== VENDOR LOGIN PAGE INITIALIZATION COMPLETE ===');
},
async loadVendor() {
vendorLoginLog.info('Loading vendor information...');
this.loading = true;
try {
const response = await apiClient.get(`/vendor/${this.vendorCode}`);
this.vendor = response;
vendorLoginLog.info('Vendor loaded successfully:', {
code: this.vendor.code,
name: this.vendor.name
});
} catch (error) {
window.LogConfig.logError(error, 'Load Vendor');
this.error = 'Failed to load vendor information';
} finally {
this.loading = false;
}
},
async handleLogin() {
vendorLoginLog.info('=== VENDOR LOGIN ATTEMPT STARTED ===');
this.clearErrors();
this.loading = true;
try {
if (!this.credentials.username) {
this.errors.username = 'Username is required';
}
if (!this.credentials.password) {
this.errors.password = 'Password is required';
}
if (Object.keys(this.errors).length > 0) {
vendorLoginLog.warn('Validation failed:', this.errors);
this.loading = false;
return;
}
vendorLoginLog.info('Calling vendor login API...');
vendorLoginLog.debug('Username:', this.credentials.username);
vendorLoginLog.debug('Vendor code:', this.vendorCode);
window.LogConfig.logApiCall('POST', '/vendor/auth/login', {
username: this.credentials.username,
vendor_code: this.vendorCode
}, 'request');
const startTime = performance.now();
const response = await apiClient.post('/vendor/auth/login', {
email_or_username: this.credentials.username,
password: this.credentials.password,
vendor_code: this.vendorCode
});
const duration = performance.now() - startTime;
window.LogConfig.logApiCall('POST', '/vendor/auth/login', {
hasToken: !!response.access_token,
user: response.user?.username
}, 'response');
window.LogConfig.logPerformance('Vendor Login', duration);
vendorLoginLog.info('Login successful!');
vendorLoginLog.debug('Storing authentication data...');
// Store token with correct key that apiClient expects
localStorage.setItem('vendor_token', response.access_token);
localStorage.setItem('currentUser', JSON.stringify(response.user));
localStorage.setItem('vendorCode', this.vendorCode);
vendorLoginLog.debug('Token stored as vendor_token in localStorage');
this.success = 'Login successful! Redirecting...';
vendorLoginLog.info('Redirecting to vendor dashboard...');
setTimeout(() => {
window.location.href = `/vendor/${this.vendorCode}/dashboard`;
}, 1000);
} catch (error) {
window.LogConfig.logError(error, 'Vendor Login');
if (error.status === 401) {
this.error = 'Invalid username or password';
} else if (error.status === 403) {
this.error = 'Your account does not have access to this vendor';
} else {
this.error = error.message || 'Login failed. Please try again.';
}
vendorLoginLog.info('Error message displayed to user:', this.error);
} finally {
this.loading = false;
vendorLoginLog.info('=== VENDOR LOGIN ATTEMPT FINISHED ===');
}
},
clearErrors() {
vendorLoginLog.debug('Clearing form errors');
this.error = '';
this.errors = {};
},
toggleDarkMode() {
vendorLoginLog.debug('Toggling dark mode...');
this.dark = !this.dark;
localStorage.setItem('theme', this.dark ? 'dark' : 'light');
vendorLoginLog.info('Dark mode:', this.dark ? 'ON' : 'OFF');
}
};
}
vendorLoginLog.info('Vendor login module loaded');