Files
orion/app/modules/tenancy/static/store/js/login.js
Samir Boulahtit d9fc52d47a
Some checks failed
CI / ruff (push) Successful in 10s
CI / validate (push) Has been cancelled
CI / dependency-scanning (push) Has been cancelled
CI / docs (push) Has been cancelled
CI / deploy (push) Has been cancelled
CI / pytest (push) Has been cancelled
feat: email verification, merchant/store password reset, seed gap fix
- Add EmailVerificationToken and UserPasswordResetToken models with migration
- Add email verification flow: verify-email page route, resend-verification API
- Block login for unverified users (EmailNotVerifiedException in auth_service)
- Add forgot-password/reset-password endpoints for merchant and store auth
- Add "Forgot Password?" links to merchant and store login pages
- Send welcome email with verification link on merchant creation
- Seed email_verification and merchant_password_reset email templates
- Fix db-reset Makefile to run all init-prod seed scripts
- Add UserAuthService to satisfy architecture validation rules
- Add 52 new tests (unit + integration) with full coverage

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-18 23:22:46 +01:00

206 lines
7.9 KiB
JavaScript

// app/static/store/js/login.js
// noqa: js-003 - Standalone login page without store layout
// noqa: js-004 - Standalone page has no currentPage sidebar highlight
/**
* Store login page logic
*/
// Create custom logger for store login page
const storeLoginLog = window.LogConfig.createLogger('STORE-LOGIN');
function storeLogin() {
return {
credentials: {
username: '',
password: ''
},
store: null,
storeCode: null,
loading: false,
checked: false,
error: '',
success: '',
errors: {},
dark: false,
async init() {
// Guard against multiple initialization
if (window._storeLoginInitialized) return;
window._storeLoginInitialized = true;
try {
storeLoginLog.info('=== STORE LOGIN PAGE INITIALIZING ===');
// Load theme
const theme = localStorage.getItem('theme');
if (theme === 'dark') {
this.dark = true;
}
storeLoginLog.debug('Dark mode:', this.dark);
// Get store code from URL path
// Supports both /store/{code}/login and /platforms/{platform}/store/{code}/login
const pathSegments = window.location.pathname.split('/').filter(Boolean);
const storeIndex = pathSegments.indexOf('store');
if (storeIndex !== -1 && pathSegments[storeIndex + 1]) {
this.storeCode = pathSegments[storeIndex + 1];
storeLoginLog.debug('Store code from URL:', this.storeCode);
await this.loadStore();
}
this.checked = true;
storeLoginLog.info('=== STORE LOGIN PAGE INITIALIZATION COMPLETE ===');
} catch (error) {
storeLoginLog.error('Failed to initialize login page:', error);
this.checked = true;
}
},
async loadStore() {
storeLoginLog.info('Loading store information...');
this.loading = true;
try {
const response = await apiClient.get(`/store/info/${this.storeCode}`);
this.store = response;
storeLoginLog.info('Store loaded successfully:', {
code: this.store.code,
name: this.store.name
});
} catch (error) {
window.LogConfig.logError(error, 'Load Store');
this.error = 'Failed to load store information';
} finally {
this.loading = false;
}
},
async handleLogin() {
storeLoginLog.info('=== STORE 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) {
storeLoginLog.warn('Validation failed:', this.errors);
this.loading = false;
return;
}
storeLoginLog.info('Calling store login API...');
storeLoginLog.debug('Username:', this.credentials.username);
storeLoginLog.debug('Store code:', this.storeCode);
window.LogConfig.logApiCall('POST', '/store/auth/login', {
username: this.credentials.username,
store_code: this.storeCode
}, 'request');
const startTime = performance.now();
const response = await apiClient.post('/store/auth/login', {
email_or_username: this.credentials.username,
password: this.credentials.password,
store_code: this.storeCode
});
const duration = performance.now() - startTime;
window.LogConfig.logApiCall('POST', '/store/auth/login', {
hasToken: !!response.access_token,
user: response.user?.username
}, 'response');
window.LogConfig.logPerformance('Store Login', duration);
storeLoginLog.info('Login successful!');
storeLoginLog.debug('Storing authentication data...');
// Store token with correct key that apiClient expects
localStorage.setItem('store_token', response.access_token);
localStorage.setItem('currentUser', JSON.stringify(response.user));
localStorage.setItem('storeCode', this.storeCode);
storeLoginLog.debug('Token stored as store_token in localStorage');
this.success = 'Login successful! Redirecting...';
// Check for last visited page (saved before logout)
const lastPage = localStorage.getItem('store_last_visited_page');
const validLastPage = lastPage &&
lastPage.startsWith(`/store/${this.storeCode}/`) &&
!lastPage.includes('/login') &&
!lastPage.includes('/onboarding');
const redirectTo = validLastPage ? lastPage : `/store/${this.storeCode}/dashboard`;
storeLoginLog.info('Last visited page:', lastPage);
storeLoginLog.info('Redirecting to:', redirectTo);
setTimeout(() => {
window.location.href = redirectTo;
}, 1000);
} catch (error) {
window.LogConfig.logError(error, 'Store 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 store';
} else {
this.error = error.message || 'Login failed. Please try again.';
}
storeLoginLog.info('Error message displayed to user:', this.error);
} finally {
this.loading = false;
storeLoginLog.info('=== STORE LOGIN ATTEMPT FINISHED ===');
}
},
// Forgot password state
showForgotPassword: false,
forgotPasswordEmail: '',
forgotPasswordLoading: false,
async handleForgotPassword() {
storeLoginLog.info('=== FORGOT PASSWORD ATTEMPT ===');
if (!this.forgotPasswordEmail.trim()) {
this.error = 'Email is required';
return;
}
this.forgotPasswordLoading = true;
this.clearErrors();
try {
await apiClient.post('/store/auth/forgot-password', {
email: this.forgotPasswordEmail.trim()
});
this.success = 'If an account exists with this email, a password reset link has been sent.';
this.forgotPasswordEmail = '';
} catch (error) {
window.LogConfig.logError(error, 'ForgotPassword');
this.error = error.message || 'Failed to send reset email. Please try again.';
} finally {
this.forgotPasswordLoading = false;
}
},
clearErrors() {
storeLoginLog.debug('Clearing form errors');
this.error = '';
this.errors = {};
},
toggleDarkMode() {
storeLoginLog.debug('Toggling dark mode...');
this.dark = !this.dark;
localStorage.setItem('theme', this.dark ? 'dark' : 'light');
storeLoginLog.info('Dark mode:', this.dark ? 'ON' : 'OFF');
}
};
}
storeLoginLog.info('Store login module loaded');