110 lines
3.4 KiB
JavaScript
110 lines
3.4 KiB
JavaScript
// app/static/vendor/js/login.js
|
|
/**
|
|
* Vendor login page logic
|
|
*/
|
|
|
|
// ✅ Use centralized logger - ONE LINE!
|
|
// Create custom logger for login page
|
|
const loginLog = window.LogConfig.createLogger('LOGIN');
|
|
|
|
function vendorLogin() {
|
|
return {
|
|
credentials: {
|
|
username: '',
|
|
password: ''
|
|
},
|
|
vendor: null,
|
|
vendorCode: null,
|
|
loading: false,
|
|
checked: false,
|
|
error: '',
|
|
success: '',
|
|
errors: {},
|
|
dark: false,
|
|
|
|
async init() {
|
|
// Load theme
|
|
const theme = localStorage.getItem('theme');
|
|
if (theme === 'dark') {
|
|
this.dark = true;
|
|
}
|
|
|
|
// Get vendor code from URL path
|
|
const pathSegments = window.location.pathname.split('/').filter(Boolean);
|
|
if (pathSegments[0] === 'vendor' && pathSegments[1]) {
|
|
this.vendorCode = pathSegments[1];
|
|
await this.loadVendor();
|
|
}
|
|
this.checked = true;
|
|
},
|
|
|
|
async loadVendor() {
|
|
this.loading = true;
|
|
try {
|
|
const response = await apiClient.get(`/vendor/${this.vendorCode}`);
|
|
this.vendor = response;
|
|
logInfo('Vendor loaded', this.vendor);
|
|
} catch (error) {
|
|
logError('Failed to load vendor', error);
|
|
this.error = 'Failed to load vendor information';
|
|
} finally {
|
|
this.loading = false;
|
|
}
|
|
},
|
|
|
|
async handleLogin() {
|
|
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) {
|
|
this.loading = false;
|
|
return;
|
|
}
|
|
|
|
const response = await apiClient.post('/vendor/auth/login', {
|
|
username: this.credentials.username,
|
|
password: this.credentials.password,
|
|
vendor_code: this.vendorCode
|
|
});
|
|
|
|
logInfo('Login successful', response);
|
|
|
|
localStorage.setItem('accessToken', response.access_token);
|
|
localStorage.setItem('currentUser', JSON.stringify(response.user));
|
|
localStorage.setItem('vendorCode', this.vendorCode);
|
|
|
|
this.success = 'Login successful! Redirecting...';
|
|
|
|
setTimeout(() => {
|
|
window.location.href = `/vendor/${this.vendorCode}/dashboard`;
|
|
}, 1000);
|
|
|
|
} catch (error) {
|
|
logError('Login failed', error);
|
|
|
|
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.';
|
|
}
|
|
} finally {
|
|
this.loading = false;
|
|
}
|
|
},
|
|
|
|
clearErrors() {
|
|
this.error = '';
|
|
this.errors = {};
|
|
}
|
|
};
|
|
} |