mirror of
https://github.com/arsvendg/Stirling-PDF.git
synced 2026-07-14 10:34:06 +02:00
# Description of Changes <img width="1569" height="980" alt="image" src="https://github.com/user-attachments/assets/dca1c227-ed84-4393-97a1-e3ce6eb1620b" /> <img width="1596" height="935" alt="image" src="https://github.com/user-attachments/assets/2003e1be-034a-4cbb-869e-6d5d912ab61d" /> <img width="1543" height="997" alt="image" src="https://github.com/user-attachments/assets/fe0c4f4b-eeee-4db4-a041-e554f350255a" /> --- ## Checklist ### General - [ ] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [ ] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md) (if applicable) - [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md) (if applicable) - [ ] I have performed a self-review of my own code - [ ] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### Translations (if applicable) - [ ] I ran [`scripts/counter_translation.py`](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/docs/counter_translation.md) ### UI Changes (if applicable) - [ ] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [ ] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md#6-testing) for more details.
160 lines
5.0 KiB
TypeScript
160 lines
5.0 KiB
TypeScript
import { useState } from 'react';
|
|
import { Modal, Stack, Text, PasswordInput, Button, Alert } from '@mantine/core';
|
|
import { useTranslation } from 'react-i18next';
|
|
import LocalIcon from '@app/components/shared/LocalIcon';
|
|
import { accountService } from '@app/services/accountService';
|
|
import { alert } from '@app/components/toast';
|
|
import { Z_INDEX_OVER_FULLSCREEN_SURFACE } from '@app/styles/zIndex';
|
|
|
|
interface FirstLoginModalProps {
|
|
opened: boolean;
|
|
onPasswordChanged: () => void;
|
|
username: string;
|
|
}
|
|
|
|
/**
|
|
* FirstLoginModal
|
|
*
|
|
* Forces first-time users to change their password.
|
|
* Cannot be dismissed until password is successfully changed.
|
|
*/
|
|
export default function FirstLoginModal({ opened, onPasswordChanged, username }: FirstLoginModalProps) {
|
|
const { t } = useTranslation();
|
|
const [currentPassword, setCurrentPassword] = useState('');
|
|
const [newPassword, setNewPassword] = useState('');
|
|
const [confirmPassword, setConfirmPassword] = useState('');
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState('');
|
|
|
|
const handleSubmit = async () => {
|
|
// Validation
|
|
if (!currentPassword || !newPassword || !confirmPassword) {
|
|
setError(t('firstLogin.allFieldsRequired', 'All fields are required'));
|
|
return;
|
|
}
|
|
|
|
if (newPassword !== confirmPassword) {
|
|
setError(t('firstLogin.passwordsDoNotMatch', 'New passwords do not match'));
|
|
return;
|
|
}
|
|
|
|
if (newPassword.length < 8) {
|
|
setError(t('firstLogin.passwordTooShort', 'Password must be at least 8 characters'));
|
|
return;
|
|
}
|
|
|
|
if (newPassword === currentPassword) {
|
|
setError(t('firstLogin.passwordMustBeDifferent', 'New password must be different from current password'));
|
|
return;
|
|
}
|
|
|
|
try {
|
|
setLoading(true);
|
|
setError('');
|
|
|
|
await accountService.changePasswordOnLogin(currentPassword, newPassword);
|
|
|
|
alert({
|
|
alertType: 'success',
|
|
title: t('firstLogin.passwordChangedSuccess', 'Password changed successfully! Please log in again.')
|
|
});
|
|
|
|
// Clear form
|
|
setCurrentPassword('');
|
|
setNewPassword('');
|
|
setConfirmPassword('');
|
|
|
|
// Wait a moment for the user to see the success message
|
|
// Then the backend will have logged them out, and onPasswordChanged will handle redirect
|
|
setTimeout(() => {
|
|
onPasswordChanged();
|
|
}, 1500);
|
|
} catch (err: any) {
|
|
console.error('Failed to change password:', err);
|
|
setError(
|
|
err.response?.data?.message ||
|
|
t('firstLogin.passwordChangeFailed', 'Failed to change password. Please check your current password.')
|
|
);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Modal
|
|
opened={opened}
|
|
onClose={() => {}} // Cannot close
|
|
title={t('firstLogin.title', 'First Time Login')}
|
|
closeOnClickOutside={false}
|
|
closeOnEscape={false}
|
|
withCloseButton={false}
|
|
centered
|
|
size="md"
|
|
zIndex={Z_INDEX_OVER_FULLSCREEN_SURFACE}
|
|
>
|
|
<Stack gap="md">
|
|
<Alert
|
|
icon={<LocalIcon icon="info-rounded" width="1rem" height="1rem" />}
|
|
title={t('firstLogin.welcomeTitle', 'Welcome!')}
|
|
color="blue"
|
|
>
|
|
<Text size="sm">
|
|
{t(
|
|
'firstLogin.welcomeMessage',
|
|
'For security reasons, you must change your password on your first login.'
|
|
)}
|
|
</Text>
|
|
</Alert>
|
|
|
|
<Text size="sm" fw={500}>
|
|
{t('firstLogin.loggedInAs', 'Logged in as')}: <strong>{username}</strong>
|
|
</Text>
|
|
|
|
{error && (
|
|
<Alert
|
|
icon={<LocalIcon icon="error-rounded" width="1rem" height="1rem" />}
|
|
title={t('firstLogin.error', 'Error')}
|
|
color="red"
|
|
>
|
|
{error}
|
|
</Alert>
|
|
)}
|
|
|
|
<PasswordInput
|
|
label={t('firstLogin.currentPassword', 'Current Password')}
|
|
placeholder={t('firstLogin.enterCurrentPassword', 'Enter your current password')}
|
|
value={currentPassword}
|
|
onChange={(e) => setCurrentPassword(e.currentTarget.value)}
|
|
required
|
|
/>
|
|
|
|
<PasswordInput
|
|
label={t('firstLogin.newPassword', 'New Password')}
|
|
placeholder={t('firstLogin.enterNewPassword', 'Enter new password (min 8 characters)')}
|
|
value={newPassword}
|
|
onChange={(e) => setNewPassword(e.currentTarget.value)}
|
|
required
|
|
/>
|
|
|
|
<PasswordInput
|
|
label={t('firstLogin.confirmPassword', 'Confirm New Password')}
|
|
placeholder={t('firstLogin.reEnterNewPassword', 'Re-enter new password')}
|
|
value={confirmPassword}
|
|
onChange={(e) => setConfirmPassword(e.currentTarget.value)}
|
|
required
|
|
/>
|
|
|
|
<Button
|
|
fullWidth
|
|
onClick={handleSubmit}
|
|
loading={loading}
|
|
disabled={!currentPassword || !newPassword || !confirmPassword}
|
|
mt="md"
|
|
>
|
|
{t('firstLogin.changePassword', 'Change Password')}
|
|
</Button>
|
|
</Stack>
|
|
</Modal>
|
|
);
|
|
}
|