Skip to content

Instantly share code, notes, and snippets.

@Muzietto
Last active August 29, 2018 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Muzietto/170ff712ec59465bc851a141f669f93f to your computer and use it in GitHub Desktop.
Save Muzietto/170ff712ec59465bc851a141f669f93f to your computer and use it in GitHub Desktop.
Sagas to write localstorage and read it back. Gotta save info in state instead...
import { call, put, takeLatest, take, race } from 'redux-saga/effects'
import {
SWITCH_BIOMETRIC_UNLOCK_REQUEST,
SWITCH_BIOMETRIC_UNLOCK_REJECTED,
SWITCH_BIOMETRIC_UNLOCK_SUCCESS,
} from './Preferences.actions'
import {
CLOSE_PASSWORD_MODAL,
OPEN_PASSWORD_MODAL,
PASSWORD_VALIDATED,
} from 'app/redux/PasswordPromptModal.actions'
import FingerprintService from 'app/native/FingerprintService'
export default () => [
takeLatest(SWITCH_BIOMETRIC_UNLOCK_REQUEST, onSwitchBiometricUnlock),
]
function* onSwitchBiometricUnlock({ payload: { enable } }) {
if (enable) {
yield call(activateBiometricUnlock)
return
}
yield call(disableBiometricUnlock)
}
function* activateBiometricUnlock() {
const isAvailable = yield call(FingerprintService.isAvailable)
if (!isAvailable) {
yield put(uiActions.pushPage({ component: pages.TurnOnFingerprintInfo, }))
yield put(SWITCH_BIOMETRIC_UNLOCK_REJECTED())
return
}
yield put(OPEN_PASSWORD_MODAL())
const {
cancel,
validPassword: {
payload: { password },
},
} = yield race({
validPassword: take(PASSWORD_VALIDATED),
cancel: take(CLOSE_PASSWORD_MODAL),
})
if (cancel) {
yield put(SWITCH_BIOMETRIC_UNLOCK_REJECTED())
return
}
try {
const token = yield call(FingerprintService.encrypt, password)
yield call(defaultStorage.setItem, 'fingerprintEncryptedPassword', token)
yield put(SWITCH_BIOMETRIC_UNLOCK_SUCCESS(true))
} catch (e) {
console.log(`It is not possible to activate the Fingerprint ${e.toString()}`)
}
}
function* disableBiometricUnlock() {
yield call(FingerprintService.delete)
yield call(defaultStorage.removeItem, 'fingerprintEncryptedPassword')
yield put(SWITCH_BIOMETRIC_UNLOCK_SUCCESS(false))
}
import { takeLatest, call, put, select, takeEvery } from 'redux-saga/effects'
import actions from './ui.actions'
import splashScreen from 'app/native/splashScreen'
import {SWITCH_BIOMETRIC_UNLOCK_SUCCESS} from 'app/ui/pages/Preferences/redux/Preferences.actions'
function* bootUi(action) {
try {
yield splashScreen.show()
const isFingerprintEnabled = yield call(storage.getItem, 'fingerprintEncryptedPassword')
yield put(SWITCH_BIOMETRIC_UNLOCK_SUCCESS(typeof isFingerprintEnabled !== 'undefined'))
} catch (e) {
yield put(actions.genericError())
} finally {
splashScreen.hide()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment