Ignore:
Timestamp:
02/22/26 22:32:07 (7 months ago)
Author:
Klimentina Efremova <klimentina08642@…>
Branches:
finki-main, main
Children:
6fea37e, 81bc7da
Parents:
4dff800
Message:

Improved employee logging in and changing password

File:
1 edited

Legend:

Unmodified
Added
Removed
  • interfejs/change-password.html

    r4dff800 r79fff4f  
    1717            <li><a href="index.html">Home</a></li>
    1818            <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>
    2020        </ul>
    2121    </nav>
    … …  
    2323
    2424<main>
    25     <div class="form-container">
     25    <div class="auth-container">
    2626        <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>
    3128
    3229        <form id="change-password-form" class="form">
    3330            <div class="form-group">
    3431                <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>
    3633                <small class="password-hint">At least 8 characters with uppercase, lowercase, number, and special character</small>
    3734            </div>
    … …  
    3936            <div class="form-group">
    4037                <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>
    4239            </div>
    4340
    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>
    4742        </form>
    4843    </div>
    … …  
    6257            </ul>
    6358        </div>
     59        <div class="footer-section">
     60            <h3>Contact</h3>
     61            <p>Email: support@handcraft.com</p>
     62        </div>
    6463    </div>
    6564    <div class="footer-bottom">
    … …  
    7069<script src="script.js"></script>
    7170<script>
    72     document.addEventListener('DOMContentLoaded', () => {
     71    document.addEventListener('DOMContentLoaded', async function() {
    7372        const urlParams = new URLSearchParams(window.location.search);
    74         const isForced = urlParams.get('forced') === 'true';
    75         let userType = 'admin'; // Default
     73        const forced = urlParams.get('forced');
     74        const redirect = urlParams.get('redirect');
    7675
    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;
    8087        }
    8188
    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        }
    8694
    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        }
    8999
    90         form.addEventListener('submit', async (e) => {
     100        // Handle form submission
     101        document.getElementById('change-password-form').addEventListener('submit', async function(e) {
    91102            e.preventDefault();
    92 
    93             // Clear previous messages
    94             errorDiv.style.display = 'none';
    95             successDiv.style.display = 'none';
    96 
    97             // Disable submit button to prevent double submission
    98             submitBtn.disabled = true;
    99             submitBtn.textContent = 'Changing Password...';
    100103
    101104            const newPassword = document.getElementById('new-password').value;
    102105            const confirmPassword = document.getElementById('confirm-password').value;
    103106
    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');
    109109                return;
    110110            }
    111111
    112             if (newPassword !== confirmPassword) {
    113                 showError('New passwords do not match');
    114                 submitBtn.disabled = false;
    115                 submitBtn.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');
    116116                return;
    117117            }
    118118
    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') || '';
    132121
    133122            try {
    134                 console.log('Sending password change request...');
    135123                const response = await fetch('/api/force-change-password', {
    136124                    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                    })
    142131                });
    143132
    144133                const data = await response.json();
    145                 console.log('Response:', data);
    146134
    147135                if (data.success) {
    148                     showSuccess('Password changed successfully! Redirecting...');
     136                    showMessage('Password changed successfully! Redirecting...', 'success');
     137                    sessionStorage.removeItem('passwordChangeRedirect');
    149138
    150                     // Clear form
    151                     form.reset();
    152 
    153                     // Redirect after short delay
     139                    // Redirect to appropriate dashboard
    154140                    setTimeout(() => {
    155141                        window.location.href = data.redirectTo || 'dashboard.html';
    156142                    }, 1500);
    157143                } 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');
    161145                }
    162146            } 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');
    167149            }
    168150        });
    169151
    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;
    222156        }
    223157    });
Note: See TracChangeset for help on using the changeset viewer.