Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Ask for current password on password change page

Blue Mouse020586eed9f5
diff --git a/backend/apps/cloud/src/user/dto/update-user.dto.ts b/backend/apps/cloud/src/user/dto/update-user.dto.ts
index 830a4e0ea..512e8c70a 100644
--- a/backend/apps/cloud/src/user/dto/update-user.dto.ts
+++ b/backend/apps/cloud/src/user/dto/update-user.dto.ts
@@ -10,6 +10,11 @@ export class UpdateUserProfileDTO {
@ApiProperty({ example: 'your_password123' })
password: string
+ @ApiProperty({ example: 'your_current_password123', required: false })
+ @IsString()
+ @IsOptional()
+ currentPassword?: string
+
@ApiProperty({ example: 'week' })
reportFrequency: string
diff --git a/backend/apps/cloud/src/user/user.controller.ts b/backend/apps/cloud/src/user/user.controller.ts
index c0a94b90e..ad77cf206 100644
--- a/backend/apps/cloud/src/user/user.controller.ts
+++ b/backend/apps/cloud/src/user/user.controller.ts
@@ -553,17 +553,30 @@ export class UserController {
@CurrentUserId() id: string,
@Req() request: Request,
): Promise<Partial<User>> {
- this.logger.log({ userDTO, id }, 'PUT /user')
+ this.logger.log({ id }, 'PUT /user')
const user = await this.userService.findOne({ where: { id } })
const shouldUpdatePassword =
!_isEmpty(userDTO.password) && _isString(userDTO.password)
if (shouldUpdatePassword) {
- // Validate new password
+ const currentPassword = userDTO.currentPassword
+
+ if (_isEmpty(currentPassword) || !_isString(currentPassword)) {
+ throw new BadRequestException('incorrectPassword')
+ }
+
+ const isPasswordValid = await this.authService.comparePassword(
+ currentPassword,
+ user.password,
+ )
+
+ if (!isPasswordValid) {
+ throw new BadRequestException('incorrectPassword')
+ }
+
this.userService.validatePassword(userDTO.password)
- // Hash new password
userDTO.password = await this.authService.hashPassword(userDTO.password)
}
diff --git a/backend/apps/community/src/user/dto/update-user.dto.ts b/backend/apps/community/src/user/dto/update-user.dto.ts
index 5154965f0..5da6b27e6 100644
--- a/backend/apps/community/src/user/dto/update-user.dto.ts
+++ b/backend/apps/community/src/user/dto/update-user.dto.ts
@@ -13,4 +13,7 @@ export class UpdateUserProfileDTO {
@ApiProperty({ example: 'newStrongPassword123', required: false })
password?: string
+
+ @ApiProperty({ example: 'currentStrongPassword123', required: false })
+ currentPassword?: string
}
diff --git a/backend/apps/community/src/user/user.controller.ts b/backend/apps/community/src/user/user.controller.ts
index cb3e4fe8b..282cda60f 100644
--- a/backend/apps/community/src/user/user.controller.ts
+++ b/backend/apps/community/src/user/user.controller.ts
@@ -341,7 +341,7 @@ export class UserController {
@CurrentUserId() id: string,
@Req() request: Request,
) {
- this.logger.log({ userDTO, id }, 'PUT /user')
+ this.logger.log({ id }, 'PUT /user')
const originalUser = await this.userService.findOne({ id })
@@ -349,11 +349,30 @@ export class UserController {
throw new BadRequestException('User not found')
}
+ const newPassword = userDTO.password
+ const shouldUpdatePassword =
+ typeof newPassword === 'string' && newPassword.length > 0
+
+ if (shouldUpdatePassword) {
+ if (!userDTO.currentPassword) {
+ throw new BadRequestException('incorrectPassword')
+ }
+
+ const isPasswordValid = await this.authService.comparePassword(
+ userDTO.currentPassword,
+ originalUser.password,
+ )
+
+ if (!isPasswordValid) {
+ throw new BadRequestException('incorrectPassword')
+ }
+ }
+
try {
- if (userDTO.password && typeof userDTO.password === 'string') {
- this.userService.validatePassword(userDTO.password)
+ if (shouldUpdatePassword) {
+ this.userService.validatePassword(newPassword)
- const hashed = await this.authService.hashPassword(userDTO.password)
+ const hashed = await this.authService.hashPassword(newPassword)
await this.userService.update(id, { password: hashed })
await Promise.all([
diff --git a/web/app/pages/UserSettings/UserSettings.tsx b/web/app/pages/UserSettings/UserSettings.tsx
index 96a946dee..cfadecf08 100644
--- a/web/app/pages/UserSettings/UserSettings.tsx
+++ b/web/app/pages/UserSettings/UserSettings.tsx
@@ -236,6 +236,7 @@ const SettingsSection = ({
)
interface Form extends Partial<User> {
+ currentPassword: string
repeat: string
password: string
email: string
@@ -385,6 +386,7 @@ const UserSettings = () => {
}
const [form, setForm] = useState<Form>(() => ({
email: '',
+ currentPassword: '',
password: '',
repeat: '',
timeFormat: user?.timeFormat || TimeFormat['12-hour'],
@@ -508,12 +510,16 @@ const UserSettings = () => {
})
}
+ if ((form.password || form.repeat) && !form.currentPassword) {
+ allErrors.currentPassword = t('profileSettings.currentPasswordRequired')
+ }
+
if (form.password !== form.repeat) {
allErrors.repeat = t('auth.common.noMatchError')
}
return allErrors
- }, [form.email, form.password, form.repeat, t])
+ }, [form.currentPassword, form.email, form.password, form.repeat, t])
const validated = _isEmpty(_keys(errors))
@@ -525,7 +531,11 @@ const UserSettings = () => {
setEmailBeenSubmitted(false)
}
- if (target.name === 'password' || target.name === 'repeat') {
+ if (
+ target.name === 'currentPassword' ||
+ target.name === 'password' ||
+ target.name === 'repeat'
+ ) {
setPasswordBeenSubmitted(false)
}
@@ -538,14 +548,18 @@ const UserSettings = () => {
const submitProfileUpdate = (
additionalData?: Record<string, unknown>,
skipValidation = false,
+ includePassword = false,
) => {
if (!skipValidation && !validated) return
const formData = new FormData()
formData.set('intent', 'update-profile')
formData.set('email', form.email || user?.email || '')
- if (form.password) formData.set('password', form.password)
- if (form.repeat) formData.set('repeat', form.repeat)
+ if (includePassword && form.currentPassword)
+ formData.set('currentPassword', form.currentPassword)
+ if (includePassword && form.password)
+ formData.set('password', form.password)
+ if (includePassword && form.repeat) formData.set('repeat', form.repeat)
if (additionalData?.timezone)
formData.set('timezone', additionalData.timezone as string)
if (additionalData?.timeFormat || form.timeFormat) {
@@ -579,7 +593,7 @@ const UserSettings = () => {
return
}
- submitProfileUpdate()
+ submitProfileUpdate(undefined, false, Boolean(form.password))
}
}
@@ -594,7 +608,7 @@ const UserSettings = () => {
const handlePasswordSubmit = () => {
setPasswordBeenSubmitted(true)
- if (errors.password || errors.repeat) return
+ if (errors.currentPassword || errors.password || errors.repeat) return
setIsPasswordChangeModalOpened(true)
}
@@ -1062,6 +1076,17 @@ const UserSettings = () => {
description={t('profileSettings.changePasswordDesc')}
>
<div className='max-w-md space-y-4'>
+ <Input
+ name='currentPassword'
+ type='password'
+ label={t('profileSettings.currentPassword')}
+ value={form.currentPassword}
+ placeholder={t('auth.common.password')}
+ onChange={handleInput}
+ error={
+ passwordBeenSubmitted ? errors.currentPassword : null
+ }
+ />
<Input
name='password'
type='password'
@@ -1084,7 +1109,9 @@ const UserSettings = () => {
<Button
size='lg'
onClick={handlePasswordSubmit}
- disabled={!form.password || !form.repeat}
+ disabled={
+ !form.currentPassword || !form.password || !form.repeat
+ }
>
{t('profileSettings.updatePassword')}
</Button>
@@ -1746,7 +1773,7 @@ const UserSettings = () => {
onSubmit={() => {
setIsPasswordChangeModalOpened(false)
passwordChangedRef.current = true
- submitProfileUpdate(undefined, true)
+ submitProfileUpdate(undefined, true, true)
}}
closeText={t('common.cancel')}
submitText={t('common.continue')}
diff --git a/web/app/routes/user-settings.tsx b/web/app/routes/user-settings.tsx
index 3ccb2d58d..6eef0fca0 100644
--- a/web/app/routes/user-settings.tsx
+++ b/web/app/routes/user-settings.tsx
@@ -79,6 +79,7 @@ export interface UserSettingsActionData {
intent?: string
error?: string
fieldErrors?: {
+ currentPassword?: string
email?: string
password?: string
repeat?: string
@@ -102,6 +103,7 @@ export async function action({ request }: ActionFunctionArgs) {
switch (intent) {
case 'update-profile': {
const email = formData.get('email')?.toString() || ''
+ const currentPassword = formData.get('currentPassword')?.toString() || ''
const password = formData.get('password')?.toString() || ''
const repeat = formData.get('repeat')?.toString() || ''
const timezone = formData.get('timezone')?.toString()
@@ -115,6 +117,9 @@ export async function action({ request }: ActionFunctionArgs) {
}
if (password) {
+ if (!currentPassword) {
+ fieldErrors.currentPassword = 'Current password is required'
+ }
if (!isValidPassword(password)) {
fieldErrors.password = `Password must be at least ${MIN_PASSWORD_CHARS} characters`
}
@@ -126,7 +131,12 @@ export async function action({ request }: ActionFunctionArgs) {
}
}
- if (fieldErrors.email || fieldErrors.password || fieldErrors.repeat) {
+ if (
+ fieldErrors.currentPassword ||
+ fieldErrors.email ||
+ fieldErrors.password ||
+ fieldErrors.repeat
+ ) {
return data<UserSettingsActionData>(
{ intent, fieldErrors },
{ status: 400 },
@@ -135,7 +145,10 @@ export async function action({ request }: ActionFunctionArgs) {
const updateData: Record<string, unknown> = {}
if (email) updateData.email = email
- if (password) updateData.password = password
+ if (password) {
+ updateData.currentPassword = currentPassword
+ updateData.password = password
+ }
if (timezone) updateData.timezone = timezone
if (timeFormat) updateData.timeFormat = timeFormat
if (reportFrequency) updateData.reportFrequency = reportFrequency
diff --git a/web/public/locales/en.json b/web/public/locales/en.json
index c5478d20d..83e81de13 100644
--- a/web/public/locales/en.json
+++ b/web/public/locales/en.json
@@ -846,6 +846,8 @@
"accountDesc": "Manage your profile and general settings.",
"passwordAuth": "Password & Authentication",
"passwordAuthDesc": "Manage access to your account.",
+ "currentPassword": "Current password",
+ "currentPasswordRequired": "Current password is required",
"newPassword": "New password",
"repeatPassword": "New password (repeated)",
"updatePassword": "Update password",