feat(core): Add user.profile.beforeUpdate hook (#8144)

Add `user.profile.beforeUpdate` hook so we can prevent user email change
if it overlaps with other users email.
This commit is contained in:
Tomi Turtiainen
2024-01-02 10:15:12 +02:00
committed by GitHub
parent ece48d6a13
commit e126ed74f3
3 changed files with 53 additions and 1 deletions

View File

@@ -51,6 +51,28 @@ export function sendSuccessResponse(
}
}
/**
* Checks if the given error is a ResponseError. It can be either an
* instance of ResponseError or an error which has the same properties.
* The latter case is for external hooks.
*/
function isResponseError(error: Error): error is ResponseError {
if (error instanceof ResponseError) {
return true;
}
if (error instanceof Error) {
return (
'httpStatusCode' in error &&
typeof error.httpStatusCode === 'number' &&
'errorCode' in error &&
typeof error.errorCode === 'number'
);
}
return false;
}
interface ErrorResponse {
code: number;
message: string;
@@ -66,7 +88,7 @@ export function sendErrorResponse(res: Response, error: Error) {
message: error.message ?? 'Unknown error',
};
if (error instanceof ResponseError) {
if (isResponseError(error)) {
if (inDevelopment) {
console.error(picocolors.red(error.httpStatusCode), error.message);
}

View File

@@ -73,6 +73,8 @@ export class MeController {
}
}
await this.externalHooks.run('user.profile.beforeUpdate', [userId, currentEmail, payload]);
await this.userService.update(userId, payload);
const user = await this.userService.findOneOrFail({ where: { id: userId } });