major refactoring adding vendor and customer features
This commit is contained in:
299
static/vendor/login.html
vendored
Normal file
299
static/vendor/login.html
vendored
Normal file
@@ -0,0 +1,299 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Vendor Login</title>
|
||||
<link rel="stylesheet" href="/static/css/shared/base.css">
|
||||
<link rel="stylesheet" href="/static/css/shared/auth.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="login-container">
|
||||
<div id="loginContent">
|
||||
<div class="login-header">
|
||||
<div class="vendor-logo">🏪</div>
|
||||
<h1>Vendor Login</h1>
|
||||
<p>Access your store management</p>
|
||||
</div>
|
||||
|
||||
<div id="vendorInfo" class="vendor-info" style="display: none;">
|
||||
Logging in to: <strong id="vendorName">-</strong>
|
||||
</div>
|
||||
|
||||
<div id="noVendorMessage" class="no-vendor-message" style="display: none;">
|
||||
<h2>⚠️ Vendor Not Found</h2>
|
||||
<p>No vendor is associated with this URL.</p>
|
||||
<a href="/" class="btn-back">← Back to Platform</a>
|
||||
</div>
|
||||
|
||||
<div id="alertBox" class="alert"></div>
|
||||
|
||||
<form id="loginForm">
|
||||
<div class="form-group">
|
||||
<label for="username">Username</label>
|
||||
<input
|
||||
type="text"
|
||||
id="username"
|
||||
name="username"
|
||||
required
|
||||
autocomplete="username"
|
||||
placeholder="Enter your username"
|
||||
>
|
||||
<div class="error-message" id="usernameError"></div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="password">Password</label>
|
||||
<input
|
||||
type="password"
|
||||
id="password"
|
||||
name="password"
|
||||
required
|
||||
autocomplete="current-password"
|
||||
placeholder="Enter your password"
|
||||
>
|
||||
<div class="error-message" id="passwordError"></div>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn-login" id="loginButton">
|
||||
Sign In
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<div class="login-footer">
|
||||
First time? Use the credentials provided by your admin.
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="/static/js/shared/api-client.js"></script>
|
||||
<script>
|
||||
const API_BASE_URL = '/api/v1';
|
||||
|
||||
// DOM Elements
|
||||
const loginForm = document.getElementById('loginForm');
|
||||
const loginButton = document.getElementById('loginButton');
|
||||
const alertBox = document.getElementById('alertBox');
|
||||
const usernameInput = document.getElementById('username');
|
||||
const passwordInput = document.getElementById('password');
|
||||
const usernameError = document.getElementById('usernameError');
|
||||
const passwordError = document.getElementById('passwordError');
|
||||
const vendorInfo = document.getElementById('vendorInfo');
|
||||
const vendorName = document.getElementById('vendorName');
|
||||
const noVendorMessage = document.getElementById('noVendorMessage');
|
||||
|
||||
// Detect vendor context
|
||||
function detectVendorContext() {
|
||||
const host = window.location.host;
|
||||
const path = window.location.pathname;
|
||||
|
||||
// Method 1: Subdomain detection (production)
|
||||
// e.g., techstore.platform.com
|
||||
const parts = host.split('.');
|
||||
if (parts.length >= 2 && parts[0] !== 'www' && parts[0] !== 'admin' && parts[0] !== 'localhost') {
|
||||
return {
|
||||
subdomain: parts[0],
|
||||
method: 'subdomain'
|
||||
};
|
||||
}
|
||||
|
||||
// Method 2: Path-based detection (development)
|
||||
// e.g., localhost:8000/vendor/techstore/login
|
||||
if (path.startsWith('/vendor/')) {
|
||||
const pathParts = path.split('/');
|
||||
if (pathParts.length >= 3 && pathParts[2]) {
|
||||
return {
|
||||
subdomain: pathParts[2],
|
||||
method: 'path'
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// Verify vendor exists
|
||||
async function verifyVendor(subdomain) {
|
||||
try {
|
||||
// Try to fetch vendor info from public API
|
||||
const response = await fetch(`${API_BASE_URL}/public/vendors/${subdomain}`);
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
return data;
|
||||
}
|
||||
|
||||
return null;
|
||||
} catch (error) {
|
||||
console.error('Error verifying vendor:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Show alert message
|
||||
function showAlert(message, type = 'error') {
|
||||
alertBox.textContent = message;
|
||||
alertBox.className = `alert alert-${type} show`;
|
||||
}
|
||||
|
||||
// Show field error
|
||||
function showFieldError(field, message) {
|
||||
const input = field === 'username' ? usernameInput : passwordInput;
|
||||
const errorDiv = field === 'username' ? usernameError : passwordError;
|
||||
|
||||
input.classList.add('error');
|
||||
errorDiv.textContent = message;
|
||||
errorDiv.classList.add('show');
|
||||
}
|
||||
|
||||
// Clear field errors
|
||||
function clearFieldErrors() {
|
||||
usernameInput.classList.remove('error');
|
||||
passwordInput.classList.remove('error');
|
||||
usernameError.classList.remove('show');
|
||||
passwordError.classList.remove('show');
|
||||
alertBox.classList.remove('show');
|
||||
}
|
||||
|
||||
// Set loading state
|
||||
function setLoadingState(loading) {
|
||||
loginButton.disabled = loading;
|
||||
|
||||
if (loading) {
|
||||
loginButton.innerHTML = '<span class="loading-spinner"></span>Signing in...';
|
||||
} else {
|
||||
loginButton.innerHTML = 'Sign In';
|
||||
}
|
||||
}
|
||||
|
||||
// Handle login
|
||||
async function handleLogin(event) {
|
||||
event.preventDefault();
|
||||
|
||||
clearFieldErrors();
|
||||
|
||||
const username = usernameInput.value.trim();
|
||||
const password = passwordInput.value;
|
||||
|
||||
// Basic validation
|
||||
if (!username) {
|
||||
showFieldError('username', 'Username is required');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!password) {
|
||||
showFieldError('password', 'Password is required');
|
||||
return;
|
||||
}
|
||||
|
||||
setLoadingState(true);
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/vendor/auth/login`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ username, password })
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(data.detail || 'Login failed');
|
||||
}
|
||||
|
||||
// Check if user is not admin (vendors should not be admin)
|
||||
if (data.user.role === 'admin') {
|
||||
throw new Error('Admin users should use the admin portal.');
|
||||
}
|
||||
|
||||
// Store token for vendor
|
||||
localStorage.setItem('vendor_token', data.access_token);
|
||||
localStorage.setItem('vendor_user', JSON.stringify(data.user));
|
||||
|
||||
// Show success message
|
||||
showAlert('Login successful! Redirecting...', 'success');
|
||||
|
||||
// Redirect to vendor dashboard
|
||||
const context = detectVendorContext();
|
||||
let dashboardUrl = '/static/vendor/dashboard.html';
|
||||
|
||||
if (context && context.method === 'path') {
|
||||
dashboardUrl = `/vendor/${context.subdomain}/dashboard`;
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
window.location.href = dashboardUrl;
|
||||
}, 1000);
|
||||
|
||||
} catch (error) {
|
||||
console.error('Login error:', error);
|
||||
showAlert(error.message || 'Login failed. Please check your credentials.');
|
||||
} finally {
|
||||
setLoadingState(false);
|
||||
}
|
||||
}
|
||||
|
||||
// Event listeners
|
||||
loginForm.addEventListener('submit', handleLogin);
|
||||
|
||||
// Clear errors on input
|
||||
usernameInput.addEventListener('input', clearFieldErrors);
|
||||
passwordInput.addEventListener('input', clearFieldErrors);
|
||||
|
||||
// Initialize page
|
||||
window.addEventListener('DOMContentLoaded', async () => {
|
||||
// Check if already logged in
|
||||
const token = localStorage.getItem('vendor_token');
|
||||
const user = localStorage.getItem('vendor_user');
|
||||
|
||||
if (token && user) {
|
||||
try {
|
||||
const userData = JSON.parse(user);
|
||||
if (userData.role !== 'admin') {
|
||||
const context = detectVendorContext();
|
||||
let dashboardUrl = '/static/vendor/dashboard.html';
|
||||
|
||||
if (context && context.method === 'path') {
|
||||
dashboardUrl = `/vendor/${context.subdomain}/dashboard`;
|
||||
}
|
||||
|
||||
window.location.href = dashboardUrl;
|
||||
return;
|
||||
}
|
||||
} catch (e) {
|
||||
localStorage.removeItem('vendor_token');
|
||||
localStorage.removeItem('vendor_user');
|
||||
}
|
||||
}
|
||||
|
||||
// Detect and verify vendor
|
||||
const context = detectVendorContext();
|
||||
|
||||
if (!context) {
|
||||
// No vendor context detected
|
||||
loginForm.style.display = 'none';
|
||||
noVendorMessage.style.display = 'block';
|
||||
return;
|
||||
}
|
||||
|
||||
// Display vendor info (for user confirmation)
|
||||
vendorName.textContent = context.subdomain;
|
||||
vendorInfo.style.display = 'block';
|
||||
|
||||
// Optionally verify vendor exists via API
|
||||
// Uncomment when public vendor API is available
|
||||
/*
|
||||
const vendor = await verifyVendor(context.subdomain);
|
||||
if (!vendor) {
|
||||
loginForm.style.display = 'none';
|
||||
noVendorMessage.style.display = 'block';
|
||||
} else {
|
||||
vendorName.textContent = vendor.name;
|
||||
}
|
||||
*/
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user