-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.html
More file actions
515 lines (440 loc) · 15.7 KB
/
Copy pathexample.html
File metadata and controls
515 lines (440 loc) · 15.7 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TGAuth Integration Example</title>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Inter', sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.container {
background: white;
border-radius: 20px;
padding: 40px;
max-width: 500px;
width: 100%;
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
h1 {
color: #333;
margin-bottom: 10px;
font-size: 32px;
}
.subtitle {
color: #666;
margin-bottom: 30px;
font-size: 16px;
}
.config-section {
background: #f8f9fa;
padding: 20px;
border-radius: 10px;
margin-bottom: 30px;
}
.config-section h3 {
color: #667eea;
margin-bottom: 15px;
font-size: 18px;
}
.input-group {
margin-bottom: 15px;
}
.input-group label {
display: block;
margin-bottom: 5px;
font-weight: 600;
color: #555;
font-size: 14px;
}
.input-group input {
width: 100%;
padding: 10px 12px;
border: 2px solid #e0e0e0;
border-radius: 8px;
font-size: 14px;
font-family: 'Courier New', monospace;
}
.input-group input:focus {
outline: none;
border-color: #667eea;
}
.btn {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 14px 32px;
border: none;
border-radius: 10px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: transform 0.2s;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
gap: 10px;
}
.btn:hover {
transform: translateY(-2px);
}
.btn:active {
transform: translateY(0);
}
.btn:disabled {
opacity: 0.6;
cursor: not-allowed;
}
.user-profile {
display: none;
text-align: center;
}
.user-profile.show {
display: block;
}
.user-profile img {
width: 120px;
height: 120px;
border-radius: 50%;
margin-bottom: 20px;
border: 4px solid #667eea;
}
.user-profile h2 {
color: #333;
margin-bottom: 10px;
}
.user-info {
background: #f8f9fa;
padding: 20px;
border-radius: 10px;
margin: 20px 0;
text-align: left;
}
.user-info-item {
display: flex;
justify-content: space-between;
padding: 10px 0;
border-bottom: 1px solid #e0e0e0;
}
.user-info-item:last-child {
border-bottom: none;
}
.user-info-item strong {
color: #555;
}
.user-info-item span {
color: #333;
font-weight: 600;
}
.btn-logout {
background: #dc3545;
margin-top: 20px;
}
.loading {
display: none;
text-align: center;
padding: 20px;
}
.loading.show {
display: block;
}
.spinner {
border: 3px solid #f3f3f3;
border-top: 3px solid #667eea;
border-radius: 50%;
width: 40px;
height: 40px;
animation: spin 1s linear infinite;
margin: 0 auto 15px;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.error {
background: #f8d7da;
color: #721c24;
padding: 12px;
border-radius: 8px;
margin-bottom: 20px;
display: none;
}
.error.show {
display: block;
}
.success {
background: #d4edda;
color: #155724;
padding: 12px;
border-radius: 8px;
margin-bottom: 20px;
display: none;
}
.success.show {
display: block;
}
.note {
background: #fff3cd;
color: #856404;
padding: 12px;
border-radius: 8px;
margin-top: 20px;
font-size: 14px;
}
</style>
</head>
<body>
<div class="container">
<h1>🔐 TGAuth Demo</h1>
<p class="subtitle">Telegram Authentication Integration</p>
<div id="errorMessage" class="error"></div>
<div id="successMessage" class="success"></div>
<!-- Configuration Section -->
<div id="configSection" class="config-section">
<h3>⚙️ Configuration</h3>
<div class="input-group">
<label for="apiUrl">API URL</label>
<input type="text" id="apiUrl" placeholder="https://your-app.vercel.app" value="https://your-app.vercel.app">
</div>
<div class="input-group">
<label for="apiKey">API Key</label>
<input type="text" id="apiKey" placeholder="tgauth_your_api_key_here">
</div>
<button class="btn" onclick="saveConfig()">💾 Save Configuration</button>
<div class="note">
<strong>Note:</strong> Get your API credentials from the TGAuth dashboard.
</div>
</div>
<!-- Login Section -->
<div id="loginSection" style="display: none;">
<button class="btn" onclick="login()">
<span>🔐 Login with Telegram</span>
</button>
</div>
<!-- Loading Section -->
<div id="loadingSection" class="loading">
<div class="spinner"></div>
<p>Waiting for Telegram authentication...</p>
<p style="font-size: 14px; color: #666; margin-top: 10px;">
Please complete the authentication in Telegram
</p>
</div>
<!-- User Profile Section -->
<div id="userProfile" class="user-profile">
<img id="userPhoto" src="" alt="Profile Photo">
<h2 id="userName"></h2>
<div class="user-info">
<div class="user-info-item">
<strong>User ID:</strong>
<span id="userId"></span>
</div>
<div class="user-info-item">
<strong>Username:</strong>
<span id="userUsername"></span>
</div>
<div class="user-info-item">
<strong>Language:</strong>
<span id="userLanguage"></span>
</div>
<div class="user-info-item">
<strong>Country:</strong>
<span id="userCountry"></span>
</div>
<div class="user-info-item">
<strong>Device:</strong>
<span id="userDevice"></span>
</div>
<div class="user-info-item">
<strong>Browser:</strong>
<span id="userBrowser"></span>
</div>
</div>
<button class="btn btn-logout" onclick="logout()">🚪 Logout</button>
</div>
</div>
<script>
// Configuration
let API_URL = '';
let API_KEY = '';
let pollInterval = null;
// Load saved config from localStorage
function loadConfig() {
const savedUrl = localStorage.getItem('tgauth_api_url');
const savedKey = localStorage.getItem('tgauth_api_key');
if (savedUrl) {
document.getElementById('apiUrl').value = savedUrl;
API_URL = savedUrl;
}
if (savedKey) {
document.getElementById('apiKey').value = savedKey;
API_KEY = savedKey;
}
if (API_URL && API_KEY) {
document.getElementById('configSection').style.display = 'none';
document.getElementById('loginSection').style.display = 'block';
checkAuthStatus();
}
}
// Save configuration
function saveConfig() {
const url = document.getElementById('apiUrl').value.trim();
const key = document.getElementById('apiKey').value.trim();
if (!url || !key) {
showError('Please fill in both API URL and API Key');
return;
}
API_URL = url;
API_KEY = key;
localStorage.setItem('tgauth_api_url', url);
localStorage.setItem('tgauth_api_key', key);
showSuccess('Configuration saved successfully!');
setTimeout(() => {
document.getElementById('configSection').style.display = 'none';
document.getElementById('loginSection').style.display = 'block';
checkAuthStatus();
}, 1000);
}
// Check if user is already authenticated
async function checkAuthStatus() {
try {
const response = await fetch(`${API_URL}/auth/status`, {
headers: { 'X-API-Key': API_KEY },
credentials: 'include'
});
const data = await response.json();
if (data.status === 'authenticated') {
showUserProfile(data.user_data);
}
} catch (error) {
console.error('Auth check failed:', error);
}
}
// Login function
async function login() {
try {
hideError();
hideSuccess();
const response = await fetch(`${API_URL}/auth/login`, {
headers: { 'X-API-Key': API_KEY }
});
if (!response.ok) {
throw new Error('Failed to initiate login');
}
const data = await response.json();
// Hide login button, show loading
document.getElementById('loginSection').style.display = 'none';
document.getElementById('loadingSection').classList.add('show');
// Open Telegram
window.location.href = data.telegram_url;
// Start polling for status
pollAuthStatus(data.session_hash);
} catch (error) {
showError('Login failed: ' + error.message);
document.getElementById('loginSection').style.display = 'block';
document.getElementById('loadingSection').classList.remove('show');
}
}
// Poll authentication status
function pollAuthStatus(hash) {
let attempts = 0;
const maxAttempts = 150; // 5 minutes (150 * 2 seconds)
pollInterval = setInterval(async () => {
attempts++;
if (attempts > maxAttempts) {
clearInterval(pollInterval);
showError('Authentication timeout. Please try again.');
document.getElementById('loadingSection').classList.remove('show');
document.getElementById('loginSection').style.display = 'block';
return;
}
try {
const response = await fetch(
`${API_URL}/auth/check?hash=${hash}`,
{ headers: { 'X-API-Key': API_KEY } }
);
const data = await response.json();
if (data.status === 'success') {
clearInterval(pollInterval);
document.getElementById('loadingSection').classList.remove('show');
showUserProfile(data.user_data);
showSuccess('Authentication successful!');
}
} catch (error) {
console.error('Poll error:', error);
}
}, 2000);
}
// Show user profile
function showUserProfile(userData) {
document.getElementById('userPhoto').src = userData.profile_photo;
document.getElementById('userName').textContent = userData.first_name;
document.getElementById('userId').textContent = userData.id;
document.getElementById('userUsername').textContent = userData.username;
document.getElementById('userLanguage').textContent = userData.language_code;
document.getElementById('userCountry').textContent = userData.country;
document.getElementById('userDevice').textContent = userData.device.device_type;
document.getElementById('userBrowser').textContent = userData.device.browser;
document.getElementById('loginSection').style.display = 'none';
document.getElementById('loadingSection').classList.remove('show');
document.getElementById('userProfile').classList.add('show');
}
// Logout function
async function logout() {
try {
await fetch(`${API_URL}/auth/logout`, {
method: 'POST',
headers: { 'X-API-Key': API_KEY },
credentials: 'include'
});
document.getElementById('userProfile').classList.remove('show');
document.getElementById('loginSection').style.display = 'block';
showSuccess('Logged out successfully!');
} catch (error) {
showError('Logout failed: ' + error.message);
}
}
// Show error message
function showError(message) {
const errorEl = document.getElementById('errorMessage');
errorEl.textContent = message;
errorEl.classList.add('show');
setTimeout(() => errorEl.classList.remove('show'), 5000);
}
// Hide error message
function hideError() {
document.getElementById('errorMessage').classList.remove('show');
}
// Show success message
function showSuccess(message) {
const successEl = document.getElementById('successMessage');
successEl.textContent = message;
successEl.classList.add('show');
setTimeout(() => successEl.classList.remove('show'), 3000);
}
// Hide success message
function hideSuccess() {
document.getElementById('successMessage').classList.remove('show');
}
// Initialize on page load
window.addEventListener('DOMContentLoaded', loadConfig);
// Clean up on page unload
window.addEventListener('beforeunload', () => {
if (pollInterval) {
clearInterval(pollInterval);
}
});
</script>
</body>
</html>