1 import type { AuthenticationStore } from '@proton/shared/lib/authentication/createAuthenticationStore';
2 import { persistSession } from '@proton/shared/lib/authentication/persistedSessionHelper';
3 import { PASSWORD_CHANGE_MESSAGE_TYPE, sendMessageToTabs } from '@proton/shared/lib/helpers/crossTab';
4 import type { Api, User } from '@proton/shared/lib/interfaces';
5 import { isSubUser } from '@proton/shared/lib/user/helpers';
7 const mutatePassword = async ({
14 authentication: AuthenticationStore;
16 clearKeyPassword: string;
20 // Don't mutate the password when signed in as sub-user
21 if (isSubUser(User)) {
24 const localID = authentication.getLocalID?.();
25 if (authentication.mode !== 'sso' || localID === undefined) {
26 authentication.setPassword(keyPassword);
30 authentication.setPassword(keyPassword);
32 const { clientKey, offlineKey } = await persistSession({
37 UID: authentication.getUID(),
39 persistent: authentication.getPersistent(),
40 trusted: authentication.getTrusted(),
41 mode: authentication.mode,
44 authentication.setClientKey(clientKey);
45 authentication.setOfflineKey(offlineKey);
47 sendMessageToTabs(PASSWORD_CHANGE_MESSAGE_TYPE, { localID, status: true });
49 sendMessageToTabs(PASSWORD_CHANGE_MESSAGE_TYPE, { localID, status: false });
50 // If persisting the password fails for some reason.
55 export default mutatePassword;