-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
288 lines (233 loc) · 10.1 KB
/
Copy pathauth.js
File metadata and controls
288 lines (233 loc) · 10.1 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
// auth.js - Authentication Logic for Login/Signup modals
// ==================== LOGIN LOGIC ====================
document.getElementById('loginForm')?.addEventListener('submit', async (e) => {
e.preventDefault();
const email = e.target.querySelector('input[type="email"]').value;
const password = e.target.querySelector('input[type="password"]').value;
try {
showLoading('login-status');
const response = await DevHubAPI.Auth.login(email, password);
showSuccess('Login successful! Redirecting...', 'login-status');
// Close modal
closeModal('loginModal');
// Redirect to appropriate dashboard
setTimeout(() => {
DevHubAPI.redirectToDashboard();
}, 1000);
} catch (error) {
showError(error.message || 'Login failed', 'login-status');
}
});
// ==================== SIGNUP LOGIC ====================
let currentStep = 1;
let signupData = {};
// Step navigation (make global)
window.nextStep = function(step) {
// Validate current step before proceeding
if (step === 2) {
if (!validateStep1()) return;
}
if (step === 3) {
// Load correct fields based on user type
const userType = document.getElementById('userType').value;
toggleUserFields(userType);
}
// Hide current step
document.getElementById(`signupStep${currentStep}`).classList.add('hidden');
document.getElementById(`dot${currentStep}`).classList.remove('active');
// Show next step
currentStep = step;
document.getElementById(`signupStep${currentStep}`).classList.remove('hidden');
document.getElementById(`dot${currentStep}`).classList.add('active');
}
window.prevStep = function(step) {
// Hide current step
document.getElementById(`signupStep${currentStep}`).classList.add('hidden');
document.getElementById(`dot${currentStep}`).classList.remove('active');
// Show previous step
currentStep = step;
document.getElementById(`signupStep${currentStep}`).classList.remove('hidden');
document.getElementById(`dot${currentStep}`).classList.add('active');
}
// Validate step 1
function validateStep1() {
const fullName = document.getElementById('fullName').value;
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
const confirmPassword = document.getElementById('confirmPassword').value;
const userType = document.getElementById('userType').value;
if (!fullName || fullName.length < 2) {
alert('Please enter your full name');
return false;
}
if (!email || !email.includes('@')) {
alert('Please enter a valid email');
return false;
}
if (!password || password.length < 6) {
alert('Password must be at least 6 characters');
return false;
}
if (password !== confirmPassword) {
alert('Passwords do not match');
return false;
}
if (!userType) {
alert('Please select user type');
return false;
}
// Store step 1 data
signupData = {
fullName,
email,
password,
userType,
};
return true;
}
// Toggle developer/client fields
function toggleUserFields(userType) {
const devFields = document.getElementById('devFields');
const clientFields = document.getElementById('clientFields');
if (userType === 'Developer') {
devFields?.classList.remove('hidden');
clientFields?.classList.add('hidden');
} else {
devFields?.classList.add('hidden');
clientFields?.classList.remove('hidden');
}
}
// Handle user type change
document.getElementById('userType')?.addEventListener('change', (e) => {
toggleUserFields(e.target.value);
});
// Generate demo data
window.generateDemo = function() {
const userType = document.getElementById('userType').value;
if (!userType) {
alert('Please select user type first');
return;
}
if (userType === 'Developer') {
document.getElementById('fullName').value = 'Demo Developer';
document.getElementById('email').value = `demo.dev.${Date.now()}@example.com`;
document.getElementById('password').value = 'password123';
document.getElementById('confirmPassword').value = 'password123';
} else {
document.getElementById('fullName').value = 'Demo Client';
document.getElementById('email').value = `demo.client.${Date.now()}@example.com`;
document.getElementById('password').value = 'password123';
document.getElementById('confirmPassword').value = 'password123';
}
}
// Final signup submission (Step 3)
document.getElementById('signupStep3')?.addEventListener('submit', async (e) => {
e.preventDefault();
try {
// Collect step 2 data
const userType = signupData.userType;
if (userType === 'Developer') {
const devFields = document.querySelectorAll('#devFields input, #devFields textarea, #devFields select');
devFields.forEach(field => {
const raw = field.placeholder || field.name || '';
const name = raw.toLowerCase().replace(/\s+/g, '').replace(/[^a-z0-9]/g, '');
if (field.value) signupData[name] = field.value;
});
// Map specific fields
signupData.username = document.querySelector('#devFields input[placeholder*="Username"]')?.value;
signupData.bio = document.querySelector('#devFields textarea')?.value;
signupData.skills = document.querySelector('#devFields input[placeholder*="Skills"]')?.value;
signupData.experienceLevel = document.querySelector('#devFields select')?.value;
signupData.yearsExperience = document.querySelector('#devFields input[placeholder*="Years"]')?.value;
signupData.portfolioUrl = document.querySelector('#devFields input[placeholder*="Portfolio"]')?.value;
signupData.location = document.querySelector('#devFields input[placeholder*="Location"]')?.value;
signupData.hourlyRate = document.querySelector('#devFields input[placeholder*="Hourly"]')?.value;
} else {
const clientFields = document.querySelectorAll('#clientFields input');
clientFields.forEach(field => {
const name = field.placeholder.toLowerCase().replace(/\s+/g, '').replace(/[^a-z0-9]/g, '');
if (field.value) {
signupData[name] = field.value;
}
});
// Map specific fields
signupData.companyName = document.querySelector('#clientFields input[placeholder*="Company"]')?.value;
signupData.companyWebsite = document.querySelector('#clientFields input[placeholder*="Website"]')?.value;
signupData.industry = document.querySelector('#clientFields input[placeholder*="Industry"]')?.value;
signupData.companySize = document.querySelector('#clientFields input[placeholder*="Size"]')?.value;
signupData.workEmail = document.querySelector('#clientFields input[placeholder*="Work Email"]')?.value;
signupData.budgetRange = document.querySelector('#clientFields input[placeholder*="Budget"]')?.value;
signupData.location = document.querySelector('#clientFields input[placeholder*="Location"]')?.value;
}
// Collect step 3 data
const step3Fields = document.querySelectorAll('#signupStep3 input, #signupStep3 select');
step3Fields.forEach(field => {
if (field.type !== 'checkbox' && field.value) {
const name = field.placeholder?.toLowerCase().replace(/\s+/g, '').replace(/[^a-z0-9]/g, '') || 'field';
signupData[name] = field.value;
}
});
signupData.phone = document.querySelector('#signupStep3 input[type="tel"]')?.value;
signupData.preferredComm = document.querySelector('#signupStep3 select')?.value;
showLoading('signup-status');
// Register user
const response = await DevHubAPI.Auth.register(signupData);
showSuccess('Registration successful! Redirecting...', 'signup-status');
// Close modal
closeModal('signupModal');
// Redirect to dashboard
setTimeout(() => {
DevHubAPI.redirectToDashboard();
}, 1000);
} catch (error) {
showError(error.message || 'Registration failed', 'signup-status');
}
});
// ==================== MODAL CONTROLS ====================
window.closeModal = function(modalId) {
document.getElementById(modalId).style.display = 'none';
// Reset signup form
if (modalId === 'signupModal') {
currentStep = 1;
signupData = {};
document.querySelectorAll('.signup-step').forEach(step => step.classList.add('hidden'));
document.getElementById('signupStep1').classList.remove('hidden');
document.querySelectorAll('.dot').forEach(dot => dot.classList.remove('active'));
document.getElementById('dot1').classList.add('active');
}
}
// Open modals
document.getElementById('openSignIn')?.addEventListener('click', () => {
document.getElementById('signupModal').style.display = 'flex';
});
document.getElementById('openLogIn')?.addEventListener('click', () => {
document.getElementById('loginModal').style.display = 'flex';
});
// Close modal when clicking outside
window.addEventListener('click', (e) => {
if (e.target.classList.contains('modal')) {
e.target.style.display = 'none';
}
});
// ==================== LOGOUT ====================
document.querySelector('.logout button')?.addEventListener('click', () => {
if (confirm('Are you sure you want to logout?')) {
DevHubAPI.Auth.logout();
}
});
// ==================== CHECK AUTH ON PAGE LOAD ====================
// Check if user is already logged in on index page
if (window.location.pathname.includes('index.html') || window.location.pathname === '/') {
if (DevHubAPI.Auth.isAuthenticated()) {
// User is logged in, show logout button or user info
const authButtons = document.querySelector('.auth-buttons');
if (authButtons) {
const user = DevHubAPI.getCurrentUser();
authButtons.innerHTML = `
<span>Welcome, ${user.fullName}!</span>
<button class="btn log-in" onclick="DevHubAPI.redirectToDashboard()">Dashboard</button>
<button class="btn sign-in" onclick="DevHubAPI.Auth.logout()">Logout</button>
`;
}
}
}