Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

Feat(fakerest): Add support for password change

Jonathan ARNAULT1aaffaefcc3b
diff --git a/src/providers/fakerest/dataProvider.ts b/src/providers/fakerest/dataProvider.ts
index 1b4ba49b..12502348 100644
--- a/src/providers/fakerest/dataProvider.ts
+++ b/src/providers/fakerest/dataProvider.ts
@@ -15,6 +15,7 @@ import {
SalesFormData,
SignUpData,
Task,
+ UpdatePasswordData,
} from '../../types';
import { getActivityLog } from '../commons/activity';
import { getCompanyAvatar } from '../commons/getCompanyAvatar';
@@ -201,6 +202,39 @@ const dataProviderWithCustomMethod: CrmDataProvider = {
}
return true;
},
+ updatePassword: async (
+ id: Identifier,
+ data: UpdatePasswordData
+ ): Promise<UpdatePasswordData> => {
+ const currentUser = await authProvider.getIdentity?.();
+ if (!currentUser) {
+ throw new Error('User not found');
+ }
+ const { data: previousData } = await dataProvider.getOne<Sale>(
+ 'sales',
+ {
+ id: currentUser.id,
+ }
+ );
+
+ if (!previousData) {
+ throw new Error('User not found');
+ }
+
+ if (previousData.password !== data.currentPassword) {
+ throw new Error('Current password is incorrect');
+ }
+
+ await dataProvider.update('sales', {
+ id,
+ data: {
+ password: data.newPassword,
+ },
+ previousData,
+ });
+
+ return data;
+ },
};
async function updateCompany(
diff --git a/src/types.ts b/src/types.ts
index 2d806a85..b2db784f 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -37,9 +37,18 @@ export type Sale = {
disabled?: boolean;
user_id: string;
- // This is a copy of the user's email, to make it easier to handle by react admin
- // DO NOT UPDATE this field directly, it should be updated by the backend
+ /**
+ * This is a copy of the user's email, to make it easier to handle by react admin
+ * DO NOT UPDATE this field directly, it should be updated by the backend
+ */
email: string;
+
+ /**
+ * This is used by the fake rest provider to store the password
+ * DO NOT USE this field in your code besides the fake rest provider
+ * @deprecated
+ */
+ password?: string;
} & Pick<RaRecord, 'id'>;
export type Company = {