fix: reset password regression (#1574)

This commit is contained in:
Dan Ribbens
2022-12-02 12:36:19 -05:00
committed by GitHub
parent 44df7f523b
commit 396ea0bd53
7 changed files with 70 additions and 28 deletions

View File

@@ -138,7 +138,7 @@ const DefaultAccount: React.FC<Props> = (props) => {
data={data}
/>
{hasSavePermission && (
<FormSubmit>{t('general:save')}</FormSubmit>
<FormSubmit buttonId="action-save">{t('general:save')}</FormSubmit>
)}
</div>
<div className={`${baseClass}__sidebar-fields`}>

View File

@@ -101,6 +101,7 @@ const Auth: React.FC<Props> = (props) => {
)}
{(!changingPassword && !requirePassword) && (
<Button
id="change-password"
size="small"
buttonStyle="secondary"
onClick={() => handleChangePassword(true)}

View File

@@ -72,13 +72,15 @@ async function update(incomingArgs: Arguments): Promise<Document> {
autosave = false,
} = args;
let { data } = args;
if (!id) {
throw new APIError('Missing ID of document to update.', httpStatus.BAD_REQUEST);
}
let { data } = args;
const { password } = data;
const shouldSaveDraft = Boolean(draftArg && collectionConfig.versions.drafts);
const shouldSavePassword = Boolean(password && collectionConfig.auth && !shouldSaveDraft);
const lean = !shouldSavePassword;
// /////////////////////////////////////
// Access
@@ -109,12 +111,13 @@ async function update(incomingArgs: Arguments): Promise<Document> {
const query = await Model.buildQuery(queryToBuild, locale);
const doc = await getLatestCollectionVersion({ payload, collection, id, query });
const doc = await getLatestCollectionVersion({ payload, collection, id, query, lean });
if (!doc && !hasWherePolicy) throw new NotFound(t);
if (!doc && hasWherePolicy) throw new Forbidden(t);
const docWithLocales: Document = JSON.parse(JSON.stringify(doc));
let docWithLocales: Document = JSON.stringify(lean ? doc : doc.toJSON({ virtuals: true }));
docWithLocales = JSON.parse(docWithLocales);
const originalDoc = await afterRead({
depth: 0,
@@ -211,9 +214,7 @@ async function update(incomingArgs: Arguments): Promise<Document> {
// Handle potential password update
// /////////////////////////////////////
const { password } = data;
if (password && collectionConfig.auth && !shouldSaveDraft) {
if (shouldSavePassword) {
await doc.setPassword(password as string);
await doc.save();
delete data.password;

View File

@@ -7,6 +7,7 @@ type Args = {
collection: Collection,
query: Record<string, unknown>
id: string | number
lean?: boolean
}
export const getLatestCollectionVersion = async <T extends TypeWithID = any>({
@@ -17,6 +18,7 @@ export const getLatestCollectionVersion = async <T extends TypeWithID = any>({
},
query,
id,
lean = true,
}: Args): Promise<T> => {
let version;
if (config.versions?.drafts) {
@@ -24,10 +26,10 @@ export const getLatestCollectionVersion = async <T extends TypeWithID = any>({
parent: id,
}, {}, {
sort: { updatedAt: 'desc' },
lean: true,
lean,
});
}
const collection = await Model.findOne(query).lean() as Document;
const collection = await Model.findOne(query, {}, { lean }) as Document;
version = await version;
if (!version || version.updatedAt < collection.updatedAt) {
return collection;