1 import { createAction } from '@reduxjs/toolkit';
2 import { c } from 'ttag';
4 import { type Lock, type LockCreateDTO, LockMode, type UnlockDTO } from '@proton/pass/lib/auth/lock/types';
5 import type { ExtraPasswordDTO, PasswordConfirmDTO } from '@proton/pass/lib/auth/password';
6 import { withCache } from '@proton/pass/store/actions/enhancers/cache';
7 import { withNotification } from '@proton/pass/store/actions/enhancers/notification';
8 import { withSettings } from '@proton/pass/store/actions/enhancers/settings';
10 extraPasswordToggleRequest,
12 passwordConfirmRequest,
14 } from '@proton/pass/store/actions/requests';
15 import { withRequest, withRequestFailure, withRequestSuccess } from '@proton/pass/store/request/enhancers';
16 import { requestActionsFactory } from '@proton/pass/store/request/flow';
17 import type { ClientEndpoint } from '@proton/pass/types';
18 import { NotificationKey } from '@proton/pass/types/worker/notification';
19 import { getErrorMessage } from '@proton/pass/utils/errors/get-error-message';
20 import { pipe } from '@proton/pass/utils/fp/pipe';
21 import { PASS_APP_NAME } from '@proton/shared/lib/constants';
23 export const signoutIntent = createAction('auth::signout::intent', (payload: { soft: boolean }) => ({ payload }));
24 export const signoutSuccess = createAction('auth::signout::success', (payload: { soft: boolean }) => ({ payload }));
26 export const lock = createAction('auth::lock', () => ({ payload: null }));
27 export const lockSync = createAction('auth::lock::sync', (lock: Lock) => ({ payload: { lock } }));
29 export const lockCreateIntent = createAction('auth::lock::create::intent', (lock: LockCreateDTO) =>
31 withRequest({ status: 'start', id: lockCreateRequest(), data: true }),
33 key: NotificationKey.LOCK,
38 return c('Info').t`Disabling auto-lock`;
39 case LockMode.SESSION:
40 return c('Info').t`Enabling PIN auto-lock`;
41 case LockMode.PASSWORD:
42 return c('Info').t`Enabling password auto-lock`;
43 case LockMode.BIOMETRICS:
44 return c('Info').t`Enabling biometrics auto-lock`;
46 })()} (${c('Info').t`Please do not close this window`})`,
49 )({ payload: { lock } })
52 export const lockCreateFailure = createAction(
53 'auth::lock::create::failure',
54 withRequestFailure((mode: LockMode, error: unknown, endpoint?: ClientEndpoint) =>
58 key: NotificationKey.LOCK,
62 return c('Info').t`Disabling auto-lock failed`;
63 case LockMode.SESSION:
64 return c('Info').t`Registering PIN lock failed`;
65 case LockMode.PASSWORD:
66 return c('Info').t`Enabling password lock failed`;
67 case LockMode.BIOMETRICS:
68 return c('Info').t`Enabling biometrics lock failed`;
72 })({ payload: {}, error })
76 export const lockCreateSuccess = createAction(
77 'auth::lock::create::success',
78 withRequestSuccess((lock: Lock, endpoint?: ClientEndpoint) =>
84 key: NotificationKey.LOCK,
88 return c('Info').t`Auto-lock successfully disabled.`;
89 case LockMode.SESSION:
90 return c('Info').t`PIN code successfully registered. Use it to unlock ${PASS_APP_NAME}`;
91 case LockMode.PASSWORD:
93 .t`Password lock successfully registered. Use it to unlock ${PASS_APP_NAME}`;
94 case LockMode.BIOMETRICS:
96 .t`Biometrics lock successfully registered. Use it to unlock ${PASS_APP_NAME}`;
101 )({ payload: { lock } })
105 export const unlock = requestActionsFactory<UnlockDTO, LockMode, LockMode>('auth::unlock')({
106 requestId: unlockRequest,
108 prepare: (error, mode) =>
110 key: NotificationKey.LOCK,
114 case LockMode.SESSION:
115 if (error instanceof Error) {
116 if (error.name === 'LockedSession') return c('Error').t`Wrong PIN code. Try again.`;
117 if (error.name === 'InactiveSession') {
118 return c('Error').t`Too many failed attempts. Please sign in again.`;
122 return c('Error').t`Unlock failure`;
126 })({ payload: null, error }),
130 export const passwordConfirm = requestActionsFactory<PasswordConfirmDTO, boolean>('auth::password::confirm')({
131 requestId: passwordConfirmRequest,
133 prepare: (error, payload) =>
136 text: getErrorMessage(error),
142 export const extraPasswordToggle = requestActionsFactory<ExtraPasswordDTO, boolean>('auth::extra-password::toggle')({
143 requestId: extraPasswordToggleRequest,
145 prepare: (payload) =>
149 text: payload.enabled
150 ? c('Info').t`Registering extra password...`
151 : c('Info').t`Removing extra password...`,
155 prepare: (enabled) =>
162 ? c('Info').t`Extra password successfully created`
163 : c('Info').t`Extra password successfully removed`,
165 )({ payload: enabled }),
168 prepare: (error, payload) =>
171 text: getErrorMessage(error),