1 import { call, put, takeLeading } from 'redux-saga/effects';
3 import { type Lock, LockMode } from '@proton/pass/lib/auth/lock/types';
4 import { lockCreateFailure, lockCreateIntent, lockCreateSuccess } from '@proton/pass/store/actions';
5 import type { WithSenderAction } from '@proton/pass/store/actions/enhancers/endpoint';
6 import type { RootSagaOptions } from '@proton/pass/store/types';
8 function* lockCreateWorker(
9 { getAuthService, getAuthStore }: RootSagaOptions,
10 { meta, payload }: WithSenderAction<ReturnType<typeof lockCreateIntent>>
13 const auth = getAuthService();
14 const authStore = getAuthStore();
16 /** check the currently registered lock in case it was mutated by another client.
17 * This can happen if the web-app and extension are logged in to the same session. */
18 yield auth.checkLock();
20 const lock: Lock = yield call(function* (): Generator<any, Lock> {
21 switch (payload.lock.mode) {
22 case LockMode.SESSION:
23 case LockMode.PASSWORD:
24 case LockMode.BIOMETRICS:
25 yield auth.createLock(payload.lock);
26 return { mode: payload.lock.mode, locked: false, ttl: payload.lock.ttl };
28 yield auth.deleteLock(authStore.getLockMode(), payload.lock.current?.secret ?? '');
29 return { mode: LockMode.NONE, locked: false };
33 yield put(lockCreateSuccess(meta.request.id, lock, meta.sender?.endpoint));
35 yield put(lockCreateFailure(meta.request.id, payload.lock.mode, error, meta.sender?.endpoint));
39 export default function* watcher(options: RootSagaOptions) {
40 yield takeLeading(lockCreateIntent.match, lockCreateWorker, options);