Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / shared / lib / authentication / mutate.ts
blob36bae87bf80af62f9b8c733c7203c98d67fd2e3b
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 ({
8     authentication,
9     keyPassword,
10     clearKeyPassword,
11     User,
12     api,
13 }: {
14     authentication: AuthenticationStore;
15     keyPassword: string;
16     clearKeyPassword: string;
17     api: Api;
18     User: User;
19 }) => {
20     // Don't mutate the password when signed in as sub-user
21     if (isSubUser(User)) {
22         return;
23     }
24     const localID = authentication.getLocalID?.();
25     if (authentication.mode !== 'sso' || localID === undefined) {
26         authentication.setPassword(keyPassword);
27         return;
28     }
29     try {
30         authentication.setPassword(keyPassword);
32         const { clientKey, offlineKey } = await persistSession({
33             api,
34             clearKeyPassword,
35             keyPassword,
36             User,
37             UID: authentication.getUID(),
38             LocalID: localID,
39             persistent: authentication.getPersistent(),
40             trusted: authentication.getTrusted(),
41             mode: authentication.mode,
42         });
44         authentication.setClientKey(clientKey);
45         authentication.setOfflineKey(offlineKey);
47         sendMessageToTabs(PASSWORD_CHANGE_MESSAGE_TYPE, { localID, status: true });
48     } catch (e: any) {
49         sendMessageToTabs(PASSWORD_CHANGE_MESSAGE_TYPE, { localID, status: false });
50         // If persisting the password fails for some reason.
51         throw e;
52     }
55 export default mutatePassword;