2022-04-22 09:49:26 -04:00
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
import {
|
|
|
|
Alert,
|
|
|
|
Button,
|
|
|
|
ControlLabel,
|
|
|
|
FormControl,
|
|
|
|
FormGroup,
|
|
|
|
} from 'react-bootstrap'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
2022-05-18 09:46:18 -04:00
|
|
|
import {
|
|
|
|
getUserFacingMessage,
|
|
|
|
postJSON,
|
|
|
|
} from '../../../infrastructure/fetch-json'
|
2022-04-22 09:49:26 -04:00
|
|
|
import getMeta from '../../../utils/meta'
|
|
|
|
import { ExposedSettings } from '../../../../../types/exposed-settings'
|
|
|
|
import { PasswordStrengthOptions } from '../../../../../types/password-strength-options'
|
|
|
|
import useAsync from '../../../shared/hooks/use-async'
|
|
|
|
|
2022-05-18 09:46:18 -04:00
|
|
|
type PasswordUpdateResult = {
|
|
|
|
message?: {
|
|
|
|
text: string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-22 09:49:26 -04:00
|
|
|
function PasswordSection() {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<h3>{t('change_password')}</h3>
|
|
|
|
<PasswordInnerSection />
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function PasswordInnerSection() {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
const { isOverleaf } = getMeta('ol-ExposedSettings') as ExposedSettings
|
|
|
|
const isExternalAuthenticationSystemUsed = getMeta(
|
|
|
|
'ol-isExternalAuthenticationSystemUsed'
|
|
|
|
) as boolean
|
|
|
|
const hasPassword = getMeta('ol-hasPassword') as boolean
|
|
|
|
|
|
|
|
if (isExternalAuthenticationSystemUsed && !isOverleaf) {
|
|
|
|
return <p>{t('password_managed_externally')}</p>
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasPassword) {
|
|
|
|
return (
|
|
|
|
<p>
|
|
|
|
<a href="/user/password/reset" target="_blank">
|
|
|
|
{t('no_existing_password')}
|
|
|
|
</a>
|
|
|
|
</p>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return <PasswordForm />
|
|
|
|
}
|
|
|
|
|
|
|
|
function PasswordForm() {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
const passwordStrengthOptions = getMeta(
|
|
|
|
'ol-passwordStrengthOptions'
|
|
|
|
) as PasswordStrengthOptions
|
|
|
|
|
|
|
|
const [currentPassword, setCurrentPassword] = useState('')
|
|
|
|
const [newPassword1, setNewPassword1] = useState('')
|
|
|
|
const [newPassword2, setNewPassword2] = useState('')
|
2022-05-18 09:46:18 -04:00
|
|
|
const { isLoading, isSuccess, isError, data, error, runAsync } =
|
|
|
|
useAsync<PasswordUpdateResult>()
|
2022-04-22 09:49:26 -04:00
|
|
|
const [isNewPasswordValid, setIsNewPasswordValid] = useState(false)
|
|
|
|
const [isFormValid, setIsFormValid] = useState(false)
|
|
|
|
|
2022-05-26 05:39:07 -04:00
|
|
|
const handleCurrentPasswordChange = (
|
|
|
|
event: React.ChangeEvent<HTMLInputElement>
|
|
|
|
) => {
|
2022-04-22 09:49:26 -04:00
|
|
|
setCurrentPassword(event.target.value)
|
|
|
|
}
|
|
|
|
|
2022-05-26 05:39:07 -04:00
|
|
|
const handleNewPassword1Change = (
|
|
|
|
event: React.ChangeEvent<HTMLInputElement>
|
|
|
|
) => {
|
2022-04-22 09:49:26 -04:00
|
|
|
setNewPassword1(event.target.value)
|
|
|
|
setIsNewPasswordValid(event.target.validity.valid)
|
|
|
|
}
|
|
|
|
|
2022-05-26 05:39:07 -04:00
|
|
|
const handleNewPassword2Change = (
|
|
|
|
event: React.ChangeEvent<HTMLInputElement>
|
|
|
|
) => {
|
2022-04-22 09:49:26 -04:00
|
|
|
setNewPassword2(event.target.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setIsFormValid(
|
|
|
|
!!currentPassword && isNewPasswordValid && newPassword1 === newPassword2
|
|
|
|
)
|
|
|
|
}, [currentPassword, newPassword1, newPassword2, isNewPasswordValid])
|
|
|
|
|
2022-05-26 05:39:07 -04:00
|
|
|
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
|
2022-04-22 09:49:26 -04:00
|
|
|
event.preventDefault()
|
|
|
|
if (!isFormValid) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
runAsync(
|
|
|
|
postJSON('/user/password/update', {
|
|
|
|
body: {
|
|
|
|
currentPassword,
|
|
|
|
newPassword1,
|
|
|
|
newPassword2,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
).catch(() => {})
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<form id="password-change-form" onSubmit={handleSubmit}>
|
|
|
|
<PasswordFormGroup
|
|
|
|
id="current-password-input"
|
|
|
|
label={t('current_password')}
|
|
|
|
value={currentPassword}
|
|
|
|
handleChange={handleCurrentPasswordChange}
|
2022-09-14 07:02:15 -04:00
|
|
|
autoComplete="current-password"
|
2022-04-22 09:49:26 -04:00
|
|
|
/>
|
|
|
|
<PasswordFormGroup
|
|
|
|
id="new-password-1-input"
|
|
|
|
label={t('new_password')}
|
|
|
|
value={newPassword1}
|
|
|
|
handleChange={handleNewPassword1Change}
|
|
|
|
minLength={passwordStrengthOptions?.length?.min || 6}
|
2022-09-14 07:02:15 -04:00
|
|
|
autoComplete="new-password"
|
2022-04-22 09:49:26 -04:00
|
|
|
/>
|
|
|
|
<PasswordFormGroup
|
|
|
|
id="new-password-2-input"
|
|
|
|
label={t('confirm_new_password')}
|
|
|
|
value={newPassword2}
|
|
|
|
handleChange={handleNewPassword2Change}
|
|
|
|
validationMessage={
|
|
|
|
newPassword1 !== newPassword2 ? t('doesnt_match') : ''
|
|
|
|
}
|
2022-09-14 07:02:15 -04:00
|
|
|
autoComplete="new-password"
|
2022-04-22 09:49:26 -04:00
|
|
|
/>
|
|
|
|
{isSuccess && data?.message?.text ? (
|
|
|
|
<FormGroup>
|
|
|
|
<Alert bsStyle="success">{data.message.text}</Alert>
|
|
|
|
</FormGroup>
|
|
|
|
) : null}
|
2022-04-25 07:05:15 -04:00
|
|
|
{isError ? (
|
2022-04-22 09:49:26 -04:00
|
|
|
<FormGroup>
|
2022-05-18 09:46:18 -04:00
|
|
|
<Alert bsStyle="danger">{getUserFacingMessage(error)}</Alert>
|
2022-04-22 09:49:26 -04:00
|
|
|
</FormGroup>
|
|
|
|
) : null}
|
|
|
|
<Button
|
|
|
|
form="password-change-form"
|
|
|
|
type="submit"
|
|
|
|
bsStyle="primary"
|
|
|
|
disabled={isLoading || !isFormValid}
|
|
|
|
>
|
|
|
|
{isLoading ? <>{t('saving')}…</> : t('change')}
|
|
|
|
</Button>
|
|
|
|
</form>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
type PasswordFormGroupProps = {
|
|
|
|
id: string
|
|
|
|
label: string
|
|
|
|
value: string
|
2022-05-26 05:39:07 -04:00
|
|
|
handleChange: (event: React.ChangeEvent<HTMLInputElement>) => void
|
2022-04-22 09:49:26 -04:00
|
|
|
minLength?: number
|
|
|
|
validationMessage?: string
|
2022-09-14 07:02:15 -04:00
|
|
|
autoComplete?: string
|
2022-04-22 09:49:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function PasswordFormGroup({
|
|
|
|
id,
|
|
|
|
label,
|
|
|
|
value,
|
|
|
|
handleChange,
|
|
|
|
minLength,
|
|
|
|
validationMessage: parentValidationMessage,
|
2022-09-14 07:02:15 -04:00
|
|
|
autoComplete,
|
2022-04-22 09:49:26 -04:00
|
|
|
}: PasswordFormGroupProps) {
|
|
|
|
const [validationMessage, setValidationMessage] = useState('')
|
|
|
|
const [hadInteraction, setHadInteraction] = useState(false)
|
|
|
|
|
2022-05-26 05:39:07 -04:00
|
|
|
const handleInvalid = (
|
|
|
|
event: React.InvalidEvent<HTMLInputElement & FormControl>
|
|
|
|
) => {
|
2022-04-22 09:49:26 -04:00
|
|
|
event.preventDefault()
|
|
|
|
}
|
|
|
|
|
2022-05-26 05:39:07 -04:00
|
|
|
const handleChangeAndValidity = (
|
|
|
|
event: React.ChangeEvent<HTMLInputElement & FormControl>
|
|
|
|
) => {
|
2022-04-22 09:49:26 -04:00
|
|
|
handleChange(event)
|
|
|
|
setHadInteraction(true)
|
|
|
|
setValidationMessage(event.target.validationMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FormGroup>
|
|
|
|
<ControlLabel htmlFor={id}>{label}</ControlLabel>
|
|
|
|
<FormControl
|
|
|
|
id={id}
|
|
|
|
type="password"
|
|
|
|
placeholder="*********"
|
2022-09-14 07:02:15 -04:00
|
|
|
autoComplete={autoComplete}
|
2022-04-22 09:49:26 -04:00
|
|
|
value={value}
|
|
|
|
data-ol-dirty={!!validationMessage}
|
|
|
|
onChange={handleChangeAndValidity}
|
|
|
|
onInvalid={handleInvalid}
|
|
|
|
required={hadInteraction}
|
|
|
|
minLength={minLength}
|
|
|
|
/>
|
|
|
|
{hadInteraction && (parentValidationMessage || validationMessage) ? (
|
|
|
|
<span className="small text-danger">
|
|
|
|
{parentValidationMessage || validationMessage}
|
|
|
|
</span>
|
|
|
|
) : null}
|
|
|
|
</FormGroup>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default PasswordSection
|