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!

fix user password update validation

Blue Mouse2e9fd9286961
diff --git a/backend/apps/community/src/user/user.controller.ts b/backend/apps/community/src/user/user.controller.ts
index 282cda60f..c984d862d 100644
--- a/backend/apps/community/src/user/user.controller.ts
+++ b/backend/apps/community/src/user/user.controller.ts
@@ -14,6 +14,7 @@ import {
} from '@nestjs/common'
import { ApiBearerAuth, ApiResponse, ApiTags } from '@nestjs/swagger'
import _isEmpty from 'lodash/isEmpty'
+import _isString from 'lodash/isString'
import _omit from 'lodash/omit'
import _isNull from 'lodash/isNull'
import _map from 'lodash/map'
@@ -354,12 +355,14 @@ export class UserController {
typeof newPassword === 'string' && newPassword.length > 0
if (shouldUpdatePassword) {
- if (!userDTO.currentPassword) {
+ const currentPassword = userDTO.currentPassword
+
+ if (_isEmpty(currentPassword) || !_isString(currentPassword)) {
throw new BadRequestException('incorrectPassword')
}
const isPasswordValid = await this.authService.comparePassword(
- userDTO.currentPassword,
+ currentPassword,
originalUser.password,
)
diff --git a/web/app/pages/UserSettings/UserSettings.tsx b/web/app/pages/UserSettings/UserSettings.tsx
index cfadecf08..7f8b384b3 100644
--- a/web/app/pages/UserSettings/UserSettings.tsx
+++ b/web/app/pages/UserSettings/UserSettings.tsx
@@ -418,6 +418,7 @@ const UserSettings = () => {
const lastHandledData = useRef<UserSettingsActionData | null>(null)
const passwordChangedRef = useRef(false)
+ const pendingPasswordChangeRef = useRef(false)
const pendingToggles = useRef<Map<string, boolean>>(new Map())
const isSubmitting = fetcher.state === 'submitting'
@@ -437,6 +438,11 @@ const UserSettings = () => {
const { intent, user: updatedUser, apiKey } = fetcher.data
if (intent === 'update-profile' && updatedUser) {
+ if (pendingPasswordChangeRef.current) {
+ pendingPasswordChangeRef.current = false
+ passwordChangedRef.current = true
+ }
+
mergeUser(updatedUser)
toast.success(t('profileSettings.updated'))
@@ -472,7 +478,7 @@ const UserSettings = () => {
toast.success(t('billing.subscriptionCancelledSuccess'))
loadUser()
}
- } else if (fetcher.data?.error) {
+ } else if (fetcher.data?.error || fetcher.data?.fieldErrors) {
setIsCancellingSubscription(false)
if (pendingToggles.current.has('live-visitors')) {
mergeUser({
@@ -488,6 +494,13 @@ const UserSettings = () => {
})
pendingToggles.current.delete('login-notifications')
}
+ if (pendingPasswordChangeRef.current) {
+ pendingPasswordChangeRef.current = false
+ passwordChangedRef.current = false
+ setIsPasswordChangeModalOpened(false)
+ }
+ if (!fetcher.data.error) return
+
const translated = t(`apiNotifications.${fetcher.data.error}`)
toast.error(
translated !== `apiNotifications.${fetcher.data.error}`
@@ -584,16 +597,19 @@ const UserSettings = () => {
}
if (form.email) setEmailBeenSubmitted(true)
- if (form.password || form.repeat) setPasswordBeenSubmitted(true)
+ const hasPasswordUpdateInput = Boolean(
+ form.password || form.currentPassword,
+ )
+ if (hasPasswordUpdateInput || form.repeat) setPasswordBeenSubmitted(true)
if (validated) {
// User is about to change their password, let's warn him if
- if (form.password && !force) {
+ if (hasPasswordUpdateInput && !force) {
setIsPasswordChangeModalOpened(true)
return
}
- submitProfileUpdate(undefined, false, Boolean(form.password))
+ submitProfileUpdate(undefined, false, hasPasswordUpdateInput)
}
}
@@ -1772,7 +1788,8 @@ const UserSettings = () => {
}}
onSubmit={() => {
setIsPasswordChangeModalOpened(false)
- passwordChangedRef.current = true
+ pendingPasswordChangeRef.current = Boolean(form.password)
+ passwordChangedRef.current = false
submitProfileUpdate(undefined, true, true)
}}
closeText={t('common.cancel')}