Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / pass / store / sagas / auth / lock-create.saga.ts
blobe1bb93de0765107c8907b9cad12ec2b36a8c6da7
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>>
11 ) {
12     try {
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 };
27                 case LockMode.NONE:
28                     yield auth.deleteLock(authStore.getLockMode(), payload.lock.current?.secret ?? '');
29                     return { mode: LockMode.NONE, locked: false };
30             }
31         });
33         yield put(lockCreateSuccess(meta.request.id, lock, meta.sender?.endpoint));
34     } catch (error) {
35         yield put(lockCreateFailure(meta.request.id, payload.lock.mode, error, meta.sender?.endpoint));
36     }
39 export default function* watcher(options: RootSagaOptions) {
40     yield takeLeading(lockCreateIntent.match, lockCreateWorker, options);