Rework locale computation on BE (#13247)
Context: Users are complaining to see their workspace in a language they don't know. This behavior is transient, happens on data model update and disappear on refresh I've check the cache for users that got the issue and did not spot any weird language ==> I think we somehow fallback the the request header locale. I feel we should always use the userWorkspace.locale, request locale should not be used in BE in my opinion except for unauthenticated endpoints. I'm also adding logs to understand the locale issue In this PR: rename user.workspaces into user.userWorkspaces which is more correct improve / simplify LOCALES typing
This commit is contained in:
@@ -528,14 +528,13 @@ export class AuthResolver {
|
||||
async updatePasswordViaResetToken(
|
||||
@Args()
|
||||
{ passwordResetToken, newPassword }: UpdatePasswordViaResetTokenInput,
|
||||
@Context() context: I18nContext,
|
||||
): Promise<InvalidatePassword> {
|
||||
const { id } =
|
||||
await this.resetPasswordService.validatePasswordResetToken(
|
||||
passwordResetToken,
|
||||
);
|
||||
|
||||
await this.authService.updatePassword(id, newPassword, context.req.locale);
|
||||
await this.authService.updatePassword(id, newPassword);
|
||||
|
||||
return await this.resetPasswordService.invalidatePasswordResetToken(id);
|
||||
}
|
||||
|
||||
+1
-1
@@ -115,7 +115,7 @@ export class SSOAuthController {
|
||||
const workspaceIdentityProvider =
|
||||
await this.workspaceSSOIdentityProviderRepository.findOne({
|
||||
where: { id: req.user.identityProviderId },
|
||||
relations: ['workspace'],
|
||||
relations: { workspace: true },
|
||||
});
|
||||
|
||||
try {
|
||||
|
||||
@@ -9,7 +9,6 @@ import { render } from '@react-email/render';
|
||||
import { addMilliseconds } from 'date-fns';
|
||||
import ms from 'ms';
|
||||
import { PasswordUpdateNotifyEmail } from 'twenty-emails';
|
||||
import { APP_LOCALES } from 'twenty-shared/translations';
|
||||
import { isDefined } from 'twenty-shared/utils';
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
@@ -142,7 +141,7 @@ export class AuthService {
|
||||
where: {
|
||||
email: input.email,
|
||||
},
|
||||
relations: ['workspaces'],
|
||||
relations: { userWorkspaces: true },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
@@ -424,7 +423,6 @@ export class AuthService {
|
||||
async updatePassword(
|
||||
userId: string,
|
||||
newPassword: string,
|
||||
locale: keyof typeof APP_LOCALES,
|
||||
): Promise<UpdatePassword> {
|
||||
if (!userId) {
|
||||
throw new AuthException(
|
||||
@@ -433,7 +431,10 @@ export class AuthService {
|
||||
);
|
||||
}
|
||||
|
||||
const user = await this.userRepository.findOneBy({ id: userId });
|
||||
const user = await this.userRepository.findOne({
|
||||
where: { id: userId },
|
||||
relations: { userWorkspaces: true },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new AuthException(
|
||||
@@ -442,6 +443,15 @@ export class AuthService {
|
||||
);
|
||||
}
|
||||
|
||||
const [firstUserWorkspace] = user.userWorkspaces;
|
||||
|
||||
if (!firstUserWorkspace) {
|
||||
throw new AuthException(
|
||||
'User does not have a workspace',
|
||||
AuthExceptionCode.USER_WORKSPACE_NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
const isPasswordValid = PASSWORD_REGEX.test(newPassword);
|
||||
|
||||
if (!isPasswordValid) {
|
||||
@@ -461,13 +471,13 @@ export class AuthService {
|
||||
userName: `${user.firstName} ${user.lastName}`,
|
||||
email: user.email,
|
||||
link: this.domainManagerService.getBaseUrl().toString(),
|
||||
locale,
|
||||
locale: firstUserWorkspace.locale,
|
||||
});
|
||||
|
||||
const html = await render(emailTemplate, { pretty: true });
|
||||
const text = await render(emailTemplate, { plainText: true });
|
||||
|
||||
i18n.activate(locale);
|
||||
i18n.activate(firstUserWorkspace.locale);
|
||||
|
||||
this.emailService.send({
|
||||
from: `${this.twentyConfigService.get(
|
||||
|
||||
Reference in New Issue
Block a user