Changeset 79fff4f for interfejs/change-password.html
- Timestamp:
- 02/22/26 22:32:07 (7 months ago)
- Branches:
- finki-main, main
- Children:
- 6fea37e, 81bc7da
- Parents:
- 4dff800
- File:
-
- 1 edited
-
interfejs/change-password.html (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
interfejs/change-password.html
r4dff800 r79fff4f 17 17 <li><a href="index.html">Home</a></li> 18 18 <li><a href="products.html">Products</a></li> 19 <li><a href=" login.html">Login</a></li>19 <li><a href="#" id="dashboard-link">Dashboard</a></li> 20 20 </ul> 21 21 </nav> … … 23 23 24 24 <main> 25 <div class=" form-container">25 <div class="auth-container"> 26 26 <h2>Change Password</h2> 27 <p class="form-subtitle" id="password-message">Please set a new password</p> 28 29 <div id="error-message" class="error-message" style="display: none; color: red; margin-bottom: 1rem; padding: 10px; background-color: #ffeeee; border-radius: 5px;"></div> 30 <div id="success-message" class="success-message" style="display: none; color: green; margin-bottom: 1rem; padding: 10px; background-color: #eeffee; border-radius: 5px;"></div> 27 <div id="password-message" class="message"></div> 31 28 32 29 <form id="change-password-form" class="form"> 33 30 <div class="form-group"> 34 31 <label for="new-password">New Password</label> 35 <input type="password" id="new-password" name="new-password"required>32 <input type="password" id="new-password" required> 36 33 <small class="password-hint">At least 8 characters with uppercase, lowercase, number, and special character</small> 37 34 </div> … … 39 36 <div class="form-group"> 40 37 <label for="confirm-password">Confirm New Password</label> 41 <input type="password" id="confirm-password" name="confirm-password"required>38 <input type="password" id="confirm-password" required> 42 39 </div> 43 40 44 <div class="form-group"> 45 <button type="submit" class="btn-primary btn-full" id="submit-btn">Change Password</button> 46 </div> 41 <button type="submit" class="btn-primary">Change Password</button> 47 42 </form> 48 43 </div> … … 62 57 </ul> 63 58 </div> 59 <div class="footer-section"> 60 <h3>Contact</h3> 61 <p>Email: support@handcraft.com</p> 62 </div> 64 63 </div> 65 64 <div class="footer-bottom"> … … 70 69 <script src="script.js"></script> 71 70 <script> 72 document.addEventListener('DOMContentLoaded', () =>{71 document.addEventListener('DOMContentLoaded', async function() { 73 72 const urlParams = new URLSearchParams(window.location.search); 74 const isForced = urlParams.get('forced') === 'true';75 let userType = 'admin'; // Default73 const forced = urlParams.get('forced'); 74 const redirect = urlParams.get('redirect'); 76 75 77 if (isForced) { 78 document.getElementById('password-message').textContent = 79 'You must change your password before continuing.'; 76 // Check if user is authenticated for password change 77 try { 78 const user = await loadUserData(); 79 if (!user) { 80 window.location.href = 'login.html'; 81 return; 82 } 83 } catch (error) { 84 console.error('Error loading user:', error); 85 window.location.href = 'login.html'; 86 return; 80 87 } 81 88 82 const form = document.getElementById('change-password-form'); 83 const submitBtn = document.getElementById('submit-btn'); 84 const errorDiv = document.getElementById('error-message'); 85 const successDiv = document.getElementById('success-message'); 89 if (forced === 'true') { 90 const messageDiv = document.getElementById('password-message'); 91 messageDiv.className = 'message warning'; 92 messageDiv.innerHTML = '⚠️ You are required to change your password before continuing.'; 93 } 86 94 87 // Check if user is authenticated for password change and get user type 88 checkAuthStatus(); 95 // Store redirect URL in session storage 96 if (redirect) { 97 sessionStorage.setItem('passwordChangeRedirect', redirect); 98 } 89 99 90 form.addEventListener('submit', async (e) => { 100 // Handle form submission 101 document.getElementById('change-password-form').addEventListener('submit', async function(e) { 91 102 e.preventDefault(); 92 93 // Clear previous messages94 errorDiv.style.display = 'none';95 successDiv.style.display = 'none';96 97 // Disable submit button to prevent double submission98 submitBtn.disabled = true;99 submitBtn.textContent = 'Changing Password...';100 103 101 104 const newPassword = document.getElementById('new-password').value; 102 105 const confirmPassword = document.getElementById('confirm-password').value; 103 106 104 // Client-side validation 105 if (!newPassword || !confirmPassword) { 106 showError('All fields are required'); 107 submitBtn.disabled = false; 108 submitBtn.textContent = 'Change Password'; 107 if (newPassword !== confirmPassword) { 108 showMessage('Passwords do not match', 'error'); 109 109 return; 110 110 } 111 111 112 if (newPassword !== confirmPassword) {113 showError('New passwords do not match');114 submitBtn.disabled = false;115 s ubmitBtn.textContent = 'Change Password';112 // Password validation 113 const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/; 114 if (!passwordRegex.test(newPassword)) { 115 showMessage('Password must have at least 8 characters, including uppercase, lowercase, number and special character', 'error'); 116 116 return; 117 117 } 118 118 119 // Validate password strength 120 const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/; 121 if (!passwordRegex.test(newPassword)) { 122 showError('Password must have at least 8 characters, including uppercase, lowercase, number and special character'); 123 submitBtn.disabled = false; 124 submitBtn.textContent = 'Change Password'; 125 return; 126 } 127 128 const formData = { 129 newPassword: newPassword, 130 confirmPassword: confirmPassword 131 }; 119 // Get redirect from session storage or URL 120 const redirectTo = sessionStorage.getItem('passwordChangeRedirect') || ''; 132 121 133 122 try { 134 console.log('Sending password change request...');135 123 const response = await fetch('/api/force-change-password', { 136 124 method: 'POST', 137 headers: { 138 'Content-Type': 'application/json' 139 }, 140 body: JSON.stringify(formData), 141 credentials: 'include' // Important: include cookies 125 headers: { 'Content-Type': 'application/json' }, 126 body: JSON.stringify({ 127 newPassword, 128 confirmPassword, 129 redirectTo: redirectTo 130 }) 142 131 }); 143 132 144 133 const data = await response.json(); 145 console.log('Response:', data);146 134 147 135 if (data.success) { 148 showSuccess('Password changed successfully! Redirecting...'); 136 showMessage('Password changed successfully! Redirecting...', 'success'); 137 sessionStorage.removeItem('passwordChangeRedirect'); 149 138 150 // Clear form 151 form.reset(); 152 153 // Redirect after short delay 139 // Redirect to appropriate dashboard 154 140 setTimeout(() => { 155 141 window.location.href = data.redirectTo || 'dashboard.html'; 156 142 }, 1500); 157 143 } else { 158 showError(data.message || 'Failed to change password'); 159 submitBtn.disabled = false; 160 submitBtn.textContent = 'Change Password'; 144 showMessage(data.message || 'Failed to change password', 'error'); 161 145 } 162 146 } catch (error) { 163 console.error('Password change error:', error); 164 showError('Network error: ' + error.message); 165 submitBtn.disabled = false; 166 submitBtn.textContent = 'Change Password'; 147 console.error('Error changing password:', error); 148 showMessage('An error occurred. Please try again.', 'error'); 167 149 } 168 150 }); 169 151 170 async function checkAuthStatus() { 171 try { 172 const response = await fetch('/api/user', { 173 credentials: 'include' 174 }); 175 const data = await response.json(); 176 177 if (!data.success) { 178 // Not authenticated, redirect to login 179 window.location.href = 'login.html'; 180 } else if (data.isTempSession) { 181 console.log('Valid temporary session for password change'); 182 if (data.user && data.user.userType) { 183 userType = data.user.userType; 184 } 185 } else { 186 // Already have full session, redirect to appropriate dashboard 187 if (data.user && data.user.userType) { 188 switch(data.user.userType) { 189 case 'admin': 190 window.location.href = 'admin.html'; 191 break; 192 case 'store_owner': 193 window.location.href = 'store-owner.html'; 194 break; 195 case 'store_employee': 196 window.location.href = 'store-employee.html'; 197 break; 198 case 'client': 199 window.location.href = 'client-dashboard.html'; 200 break; 201 default: 202 window.location.href = 'dashboard.html'; 203 } 204 } 205 } 206 } catch (error) { 207 console.error('Auth check error:', error); 208 } 209 } 210 211 function showError(message) { 212 errorDiv.textContent = message; 213 errorDiv.style.display = 'block'; 214 setTimeout(() => { 215 errorDiv.style.display = 'none'; 216 }, 5000); 217 } 218 219 function showSuccess(message) { 220 successDiv.textContent = message; 221 successDiv.style.display = 'block'; 152 function showMessage(msg, type) { 153 const messageDiv = document.getElementById('password-message'); 154 messageDiv.className = 'message ' + type; 155 messageDiv.textContent = msg; 222 156 } 223 157 });
Note:
See TracChangeset
for help on using the changeset viewer.
