152 lines
5.5 KiB
JavaScript
152 lines
5.5 KiB
JavaScript
// app/static/vendor/js/login.js
|
|
/**
|
|
* 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', {
|
|
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...');
|
|
|
|
localStorage.setItem('accessToken', response.access_token);
|
|
localStorage.setItem('currentUser', JSON.stringify(response.user));
|
|
localStorage.setItem('vendorCode', this.vendorCode);
|
|
|
|
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');
|