Spaces:
Running

testmaster-pro / components /auth-modal.js
Maqsadov's picture
ro'yxatdan otishni telegram malumotlari orqali yani bot orqali qilib ber.Keyin titul qosh test yaratish bo'limiga
d410619 verified
Raw History Blame Contribute Delete
6.66 kB
class AuthModal extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
<style>
.modal {
display: none;
position: fixed;
z-index: 1000;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
}
.modal-content {
background-color: #fff;
margin: 15% auto;
padding: 30px;
border-radius: 12px;
width: 90%;
max-width: 500px;
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
position: relative;
animation: modalopen 0.4s;
}
@keyframes modalopen {
from {opacity: 0; transform: translateY(-60px);}
to {opacity: 1; transform: translateY(0);}
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
cursor: pointer;
position: absolute;
top: 15px;
right: 20px;
}
.close:hover {
color: #000;
}
h2 {
margin-top: 0;
color: #333;
text-align: center;
}
.telegram-login {
display: flex;
flex-direction: column;
align-items: center;
gap: 20px;
}
.telegram-btn {
display: flex;
align-items: center;
justify-content: center;
gap: 10px;
background-color: #0088cc;
color: white;
padding: 12px 24px;
border-radius: 8px;
text-decoration: none;
font-weight: 500;
transition: background-color 0.3s;
border: none;
cursor: pointer;
font-size: 16px;
}
.telegram-btn:hover {
background-color: #0077b3;
}
.telegram-btn i {
font-size: 20px;
}
.user-info {
text-align: center;
padding: 20px 0;
}
.user-avatar {
width: 80px;
height: 80px;
border-radius: 50%;
object-fit: cover;
margin: 0 auto 15px;
border: 3px solid #0088cc;
}
.logout-btn {
background-color: #ff4757;
color: white;
padding: 10px 20px;
border: none;
border-radius: 6px;
cursor: pointer;
font-weight: 500;
margin-top: 15px;
}
.logout-btn:hover {
background-color: #e8414d;
}
.auth-status {
text-align: center;
padding: 15px;
background-color: #f1f2f6;
border-radius: 8px;
margin-bottom: 20px;
}
</style>
<div class="modal" id="authModal">
<div class="modal-content">
<span class="close">&times;</span>
<h2>Hisobga kirish</h2>
<div id="authContent">
<!-- Content will be dynamically inserted here -->
</div>
</div>
</div>
`;
this.setupEventListeners();
}
setupEventListeners() {
const modal = this.shadowRoot.getElementById('authModal');
const closeBtn = this.shadowRoot.querySelector('.close');
closeBtn.addEventListener('click', () => {
this.closeModal();
});
window.addEventListener('click', (event) => {
if (event.target === modal) {
this.closeModal();
}
});
// Listen for auth state changes
window.addEventListener('authStateChange', () => {
this.updateAuthContent();
});
}
openModal() {
this.shadowRoot.getElementById('authModal').style.display = 'block';
this.updateAuthContent();
}
closeModal() {
this.shadowRoot.getElementById('authModal').style.display = 'none';
}
updateAuthContent() {
const authContent = this.shadowRoot.getElementById('authContent');
const user = this.getCurrentUser();
if (user) {
authContent.innerHTML = `
<div class="user-info">
<img src="${user.photo_url || 'https://static.photos/people/320x240/1'}" alt="Avatar" class="user-avatar">
<h3>${user.first_name} ${user.last_name || ''}</h3>
<p>@${user.username || 'Foydalanuvchi'}</p>
<p>ID: ${user.id}</p>
<button class="logout-btn">Chiqish</button>
</div>
`;
this.shadowRoot.querySelector('.logout-btn').addEventListener('click', () => {
this.logout();
});
} else {
authContent.innerHTML = `
<div class="telegram-login">
<p>Telegram orqali hisobingizga kiring:</p>
<button class="telegram-btn" id="telegramAuthBtn">
<i>Telegram</i>
Telegram orqali kirish
</button>
</div>
`;
this.shadowRoot.getElementById('telegramAuthBtn').addEventListener('click', () => {
this.initiateTelegramAuth();
});
}
}
getCurrentUser() {
return JSON.parse(localStorage.getItem('telegramUser')) || null;
}
initiateTelegramAuth() {
// In a real implementation, this would redirect to Telegram Login Widget
// For demo purposes, we'll simulate a successful login
const fakeUser = {
id: Math.floor(10000000 + Math.random() * 90000000),
first_name: "Test",
last_name: "User",
username: "testuser",
photo_url: "https://static.photos/people/320x240/2",
auth_date: Math.floor(Date.now() / 1000)
};
this.loginUser(fakeUser);
}
loginUser(user) {
localStorage.setItem('telegramUser', JSON.stringify(user));
const event = new CustomEvent('authStateChange');
window.dispatchEvent(event);
this.closeModal();
// Show success message
const originalContent = this.shadowRoot.getElementById('authContent').innerHTML;
this.shadowRoot.getElementById('authContent').innerHTML = `
<div class="auth-status" style="color: #2ed573;">
<p>✅ Muvaffaqiyatli kirish!</p>
</div>
`;
setTimeout(() => {
this.shadowRoot.getElementById('authContent').innerHTML = originalContent;
this.updateAuthContent();
}, 2000);
}
logout() {
localStorage.removeItem('telegramUser');
const event = new CustomEvent('authStateChange');
window.dispatchEvent(event);
this.updateAuthContent();
}
}
customElements.define('auth-modal', AuthModal);