| |
| const AUTH_KEY = 'onco.auth'; |
|
|
| |
| const DEMO_USER = { |
| id: 'demo-001', |
| name: 'Dev Karan', |
| email: 'demo@agilisium.com', |
| password: 'Demo@123', |
| role: 'researcher', |
| avatar: 'DK' |
| }; |
|
|
| |
| const auth = { |
| |
| login(email, password, rememberMe = false) { |
| return new Promise((resolve) => { |
| |
| setTimeout(() => { |
| if (email === DEMO_USER.email && password === DEMO_USER.password) { |
| const user = { |
| id: DEMO_USER.id, |
| name: DEMO_USER.name, |
| email: DEMO_USER.email, |
| role: DEMO_USER.role, |
| avatar: DEMO_USER.avatar, |
| token: 'token_' + Date.now(), |
| expiresAt: Date.now() + (7 * 24 * 60 * 60 * 1000) |
| }; |
| |
| localStorage.setItem(AUTH_KEY, JSON.stringify(user)); |
| resolve({ success: true, user }); |
| } else { |
| resolve({ success: false, error: 'Invalid email or password' }); |
| } |
| }, 500); |
| }); |
| }, |
|
|
| |
| loginAsDemo() { |
| return this.login(DEMO_USER.email, DEMO_USER.password, true); |
| }, |
|
|
| |
| logout() { |
| localStorage.removeItem(AUTH_KEY); |
| this.showToast('You\'re signed out.', 'info'); |
| }, |
|
|
| |
| getUser() { |
| try { |
| const userData = localStorage.getItem(AUTH_KEY); |
| if (!userData) return null; |
| |
| const user = JSON.parse(userData); |
| |
| |
| if (user.expiresAt && Date.now() > user.expiresAt) { |
| this.logout(); |
| return null; |
| } |
| |
| return user; |
| } catch (error) { |
| return null; |
| } |
| }, |
|
|
| |
| isAuthenticated() { |
| return this.getUser() !== null; |
| }, |
|
|
| |
| showToast(message, type = 'info') { |
| |
| const existingToasts = document.querySelectorAll('.toast'); |
| existingToasts.forEach(toast => toast.remove()); |
|
|
| const toast = document.createElement('div'); |
| toast.className = `toast toast-${type}`; |
| toast.textContent = message; |
| |
| Object.assign(toast.style, { |
| position: 'fixed', |
| top: '20px', |
| right: '20px', |
| padding: '12px 20px', |
| borderRadius: '8px', |
| color: 'white', |
| fontWeight: '500', |
| zIndex: '10000', |
| transform: 'translateX(100%)', |
| transition: 'transform 0.3s ease', |
| maxWidth: '300px', |
| wordWrap: 'break-word', |
| boxShadow: '0 4px 12px rgba(0, 0, 0, 0.15)' |
| }); |
|
|
| const colors = { |
| success: '#10B981', |
| error: '#EF4444', |
| info: '#3B82F6' |
| }; |
| toast.style.backgroundColor = colors[type]; |
|
|
| document.body.appendChild(toast); |
|
|
| |
| setTimeout(() => { |
| toast.style.transform = 'translateX(0)'; |
| }, 100); |
|
|
| |
| setTimeout(() => { |
| toast.style.transform = 'translateX(100%)'; |
| setTimeout(() => { |
| if (toast.parentNode) { |
| toast.parentNode.removeChild(toast); |
| } |
| }, 300); |
| }, 3000); |
| } |
| }; |
|
|
| |
| window.auth = auth; |
|
|