File size: 3,782 Bytes
390cffd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | // Simple authentication system for Agilisium Co-Lab
const AUTH_KEY = 'onco.auth';
// Demo user data
const DEMO_USER = {
id: 'demo-001',
name: 'Dev Karan',
email: 'demo@agilisium.com',
password: 'Demo@123',
role: 'researcher',
avatar: 'DK'
};
// Auth helpers
const auth = {
// Login with email and password
login(email, password, rememberMe = false) {
return new Promise((resolve) => {
// Simulate network delay
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) // 7 days
};
localStorage.setItem(AUTH_KEY, JSON.stringify(user));
resolve({ success: true, user });
} else {
resolve({ success: false, error: 'Invalid email or password' });
}
}, 500);
});
},
// Quick demo login
loginAsDemo() {
return this.login(DEMO_USER.email, DEMO_USER.password, true);
},
// Logout
logout() {
localStorage.removeItem(AUTH_KEY);
this.showToast('You\'re signed out.', 'info');
},
// Get current user
getUser() {
try {
const userData = localStorage.getItem(AUTH_KEY);
if (!userData) return null;
const user = JSON.parse(userData);
// Check if session expired
if (user.expiresAt && Date.now() > user.expiresAt) {
this.logout();
return null;
}
return user;
} catch (error) {
return null;
}
},
// Check if authenticated
isAuthenticated() {
return this.getUser() !== null;
},
// Show toast notification
showToast(message, type = 'info') {
// Remove existing toasts
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);
// Animate in
setTimeout(() => {
toast.style.transform = 'translateX(0)';
}, 100);
// Remove after delay
setTimeout(() => {
toast.style.transform = 'translateX(100%)';
setTimeout(() => {
if (toast.parentNode) {
toast.parentNode.removeChild(toast);
}
}, 300);
}, 3000);
}
};
// Make auth globally available
window.auth = auth;
|