diff --git a/app/sagas/init.js b/app/sagas/init.js index 2d29e48c8..7412f436f 100644 --- a/app/sagas/init.js +++ b/app/sagas/init.js @@ -18,7 +18,7 @@ import { import { isIOS } from '../utils/deviceInfo'; import database from '../lib/database'; import protectedFunction from '../lib/methods/helpers/protectedFunction'; -import localAuthenticate from '../utils/localAuthentication'; +import { localAuthenticate } from '../utils/localAuthentication'; export const initLocalSettings = function* initLocalSettings() { const sortPreferences = yield RocketChat.getSortPreferences(); diff --git a/app/sagas/state.js b/app/sagas/state.js index 4c1cc9630..3c3d94c9e 100644 --- a/app/sagas/state.js +++ b/app/sagas/state.js @@ -4,7 +4,7 @@ import { FOREGROUND, BACKGROUND } from 'redux-enhancer-react-native-appstate'; import RocketChat from '../lib/rocketchat'; import { setBadgeCount } from '../notifications/push'; import log from '../utils/log'; -import localAuthenticate from '../utils/localAuthentication'; +import { localAuthenticate, saveLastLocalAuthenticationSession } from '../utils/localAuthentication'; import * as actions from '../actions'; const appHasComeBackToForeground = function* appHasComeBackToForeground() { @@ -39,7 +39,10 @@ const appHasComeBackToBackground = function* appHasComeBackToBackground() { return; } try { - return yield RocketChat.setUserPresenceAway(); + yield RocketChat.setUserPresenceAway(); + + const server = yield select(state => state.server.server); + yield saveLastLocalAuthenticationSession(server); } catch (e) { log(e); } diff --git a/app/utils/localAuthentication.js b/app/utils/localAuthentication.js index 468acf60a..8f3150ed3 100644 --- a/app/utils/localAuthentication.js +++ b/app/utils/localAuthentication.js @@ -1,8 +1,26 @@ import * as LocalAuthentication from 'expo-local-authentication'; +import moment from 'moment'; import database from '../lib/database'; -const localAuthenticate = async(server) => { +export const saveLastLocalAuthenticationSession = async(server) => { + console.log('saveLastLocalAuthenticationSession -> server', server); + const serversDB = database.servers; + const serversCollection = serversDB.collections.get('servers'); + await serversDB.action(async() => { + try { + const serverRecord = await serversCollection.find(server); + console.log('saveLastLocalAuthenticationSession -> serverRecord', serverRecord); + await serverRecord.update((record) => { + record.lastLocalAuthenticatedSession = new Date(); + }); + } catch (e) { + // Do nothing + } + }); +}; + +export const localAuthenticate = async(server) => { const serversDB = database.servers; const serversCollection = serversDB.collections.get('servers'); @@ -14,12 +32,16 @@ const localAuthenticate = async(server) => { return Promise.reject(); } - // if (serverRecord?.lastLocalAuthenticatedSession) { - - // } - - const authResult = await LocalAuthentication.authenticateAsync(); - return Promise.resolve(authResult?.success); + const diffToLastSession = moment().diff(serverRecord?.lastLocalAuthenticatedSession, 'seconds'); + console.log('localAuthenticate -> diffToLastSession', diffToLastSession); + if (diffToLastSession >= 5) { + const authResult = await LocalAuthentication.authenticateAsync(); + if (authResult?.success) { + await saveLastLocalAuthenticationSession(server); + } + return Promise.resolve(authResult?.success); + } else { + await saveLastLocalAuthenticationSession(server); + } + return Promise.resolve(true); }; - -export default localAuthenticate; diff --git a/app/views/RoomsListView/ServerDropdown.js b/app/views/RoomsListView/ServerDropdown.js index 5b8d79eec..52e043c02 100644 --- a/app/views/RoomsListView/ServerDropdown.js +++ b/app/views/RoomsListView/ServerDropdown.js @@ -7,7 +7,6 @@ import { connect } from 'react-redux'; import equal from 'deep-equal'; import { withNavigation } from 'react-navigation'; import RNUserDefaults from 'rn-user-defaults'; -import * as LocalAuthentication from 'expo-local-authentication'; import { toggleServerDropdown as toggleServerDropdownAction } from '../../actions/rooms'; import { selectServerRequest as selectServerRequestAction } from '../../actions/server'; @@ -23,7 +22,7 @@ import { withTheme } from '../../theme'; import { KEY_COMMAND, handleCommandSelectServer } from '../../commands'; import { isTablet } from '../../utils/deviceInfo'; import { withSplit } from '../../split'; -import localAuthenticate from '../../utils/localAuthentication'; +import { localAuthenticate } from '../../utils/localAuthentication'; const ROW_HEIGHT = 68; const ANIMATION_DURATION = 200;