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:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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 } });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user