File size: 10,576 Bytes
20f7144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ورود به پلتفرم آموزشی - RewardApp</title>
    <meta name="description" content="ورود به پلتفرم آموزش توسعه وب">
    
    <!-- استایل‌ها -->
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="css/auth.css">
    <link rel="stylesheet" href="css/responsive.css">
    
    <style>
        .auth-container {
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            padding: 20px;
        }
        
        .auth-card {
            background: white;
            border-radius: 20px;
            padding: 40px;
            box-shadow: 0 20px 60px rgba(0,0,0,0.1);
            width: 100%;
            max-width: 400px;
        }
        
        .auth-logo {
            text-align: center;
            margin-bottom: 30px;
        }
        
        .auth-logo h1 {
            color: #4a5568;
            margin-bottom: 10px;
            font-size: 24px;
        }
        
        .auth-logo p {
            color: #718096;
            font-size: 14px;
        }
        
        .auth-form .form-group {
            margin-bottom: 20px;
        }
        
        .auth-form label {
            display: block;
            margin-bottom: 8px;
            color: #4a5568;
            font-weight: 600;
        }
        
        .auth-form input {
            width: 100%;
            padding: 12px 16px;
            border: 2px solid #e2e8f0;
            border-radius: 10px;
            font-size: 16px;
            transition: all 0.3s ease;
        }
        
        .auth-form input:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }
        
        .btn-auth {
            width: 100%;
            padding: 12px;
            background: #667eea;
            color: white;
            border: none;
            border-radius: 10px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .btn-auth:hover {
            background: #5a67d8;
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
        }
        
        .auth-links {
            text-align: center;
            margin-top: 20px;
            padding-top: 20px;
            border-top: 1px solid #e2e8f0;
        }
        
        .auth-links a {
            color: #667eea;
            text-decoration: none;
            margin: 0 10px;
            transition: color 0.3s ease;
        }
        
        .auth-links a:hover {
            color: #5a67d8;
            text-decoration: underline;
        }
        
        .error-message {
            background: #fed7d7;
            color: #742a2a;
            padding: 10px;
            border-radius: 8px;
            margin-bottom: 20px;
            text-align: center;
            display: none;
        }
        
        .success-message {
            background: #c6f6d5;
            color: #22543d;
            padding: 10px;
            border-radius: 8px;
            margin-bottom: 20px;
            text-align: center;
            display: none;
        }
    </style>
</head>
<body>
    <div class="auth-container">
        <div class="auth-card">
            <div class="auth-logo">
                <h1>🎓 RewardApp</h1>
                <p>پلتفرم آموزش توسعه وب</p>
            </div>
            
            <div id="errorMessage" class="error-message"></div>
            <div id="successMessage" class="success-message"></div>
            
            <form id="loginForm" class="auth-form">
                <div class="form-group">
                    <label for="username">نام کاربری</label>
                    <input 
                        type="text" 
                        id="username" 
                        name="username" 
                        placeholder="نام کاربری خود را وارد کنید"
                        required
                    >
                </div>
                
                <div class="form-group">
                    <label for="password">رمز عبور</label>
                    <input 
                        type="password" 
                        id="password" 
                        name="password" 
                        placeholder="رمز عبور خود را وارد کنید"
                        required
                    >
                </div>
                
                <button type="submit" class="btn-auth">ورود به سیستم</button>
            </form>
            
            <div class="auth-links">
                <a href="register.html">حساب کاربری ندارید؟ ثبت نام</a>
                <br>
                <a href="index.html">بازگشت به صفحه اصلی</a>
            </div>
            
            <div class="demo-accounts" style="margin-top: 30px; padding-top: 20px; border-top: 1px solid #e2e8f0;">
                <h3 style="text-align: center; color: #718096; font-size: 14px; margin-bottom: 15px;">حساب‌های دمو:</h3>
                <div style="display: grid; gap: 10px; font-size: 12px;">
                    <div style="display: flex; justify-content: space-between;">
                        <span>دانشجو:</span>
                        <span><strong>student</strong> / <strong>password123</strong></span>
                    </div>
                    <div style="display: flex; justify-content: space-between;">
                        <span>مدیر:</span>
                        <span><strong>admin</strong> / <strong>admin123</strong></span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- اسکریپت‌های مورد نیاز -->
    <script src="js/auth/authentication.js"></script>
    <script src="js/auth/authorization.js"></script>
    <script src="js/auth/user_management.js"></script>
    <script src="js/middleware/validation_middleware.js"></script>
    
    <script>
        class LoginPage {
            constructor() {
                this.init();
            }
            
            init() {
                this.setupEventListeners();
                this.checkExistingAuth();
            }
            
            setupEventListeners() {
                const loginForm = document.getElementById('loginForm');
                if (loginForm) {
                    loginForm.addEventListener('submit', (e) => {
                        e.preventDefault();
                        this.handleLogin();
                    });
                }
                
                // مدیریت نمایش/پنهان کردن پیام‌ها
                document.addEventListener('click', () => {
                    this.hideMessages();
                });
            }
            
            checkExistingAuth() {
                if (authManager.isAuthenticated()) {
                    this.showMessage('شما قبلاً وارد شده‌اید. در حال انتقال...', 'success');
                    setTimeout(() => {
                        window.location.href = 'index.html';
                    }, 2000);
                }
            }
            
            async handleLogin() {
                const formData = new FormData(document.getElementById('loginForm'));
                const data = {
                    username: formData.get('username').trim(),
                    password: formData.get('password')
                };
                
                // اعتبارسنجی
                const validation = ValidationMiddleware.validateLoginData(data);
                if (!validation.isValid) {
                    this.showMessage(validation.errors.join(' • '), 'error');
                    return;
                }
                
                // انجام ورود
                const result = authManager.loginUser(data.username, data.password);
                
                if (result.success) {
                    this.showMessage('ورود موفقیت‌آمیز! در حال انتقال...', 'success');
                    
                    // ذخیره توکن و کاربر
                    authManager.setToken(result.token);
                    authManager.setCurrentUser(result.user);
                    
                    setTimeout(() => {
                        window.location.href = 'index.html';
                    }, 1500);
                } else {
                    this.showMessage(result.message || 'خطا در ورود به سیستم', 'error');
                }
            }
            
            showMessage(message, type = 'error') {
                const errorElement = document.getElementById('errorMessage');
                const successElement = document.getElementById('successMessage');
                
                if (type === 'error') {
                    errorElement.textContent = message;
                    errorElement.style.display = 'block';
                    successElement.style.display = 'none';
                } else {
                    successElement.textContent = message;
                    successElement.style.display = 'block';
                    errorElement.style.display = 'none';
                }
                
                // پنهان کردن خودکار پیام بعد از 5 ثانیه
                setTimeout(() => {
                    this.hideMessages();
                }, 5000);
            }
            
            hideMessages() {
                document.getElementById('errorMessage').style.display = 'none';
                document.getElementById('successMessage').style.display = 'none';
            }
        }
        
        // راه‌اندازی صفحه ورود هنگام بارگذاری
        document.addEventListener('DOMContentLoaded', () => {
            new LoginPage();
        });
        
        // نمایش خطاهای عمومی
        window.addEventListener('error', (event) => {
            console.error('Global error:', event.error);
        });
    </script>
</body>
</html>