Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/lib/rocketchat.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,17 @@ const RocketChat = {
message: I18n.t('Not_RC_Server', { contact: I18n.t('Contact_your_server_admin') })
};
},
async getServerTimeSync(server) {
try {
const response = await RNFetchBlob.fetch('GET', `${server}/_timesync`);
Comment thread
diegolmello marked this conversation as resolved.
Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This request takes 1600ms on https://mobile.rocket.chat, so we should be careful on the impact.
If screen lock is enabled, we do it right on app open.
Pay attention on the white screen after the splash screen is hidden.

before.mp4
after.mp4

A good solution for this is to move RNBootSplash.hide() to after the call.

if (response?.data) {
return parseInt(response.data);
}
return null;
} catch {
return null;
}
},
stopListener(listener) {
return listener && listener.stop();
},
Expand Down
18 changes: 14 additions & 4 deletions app/utils/localAuthentication.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as LocalAuthentication from 'expo-local-authentication';
import moment from 'moment';
import RNBootSplash from 'react-native-bootsplash';
import AsyncStorage from '@react-native-community/async-storage';
import { sha256 } from 'js-sha256';

import UserPreferences from '../lib/userPreferences';
import store from '../lib/createStore';
import database from '../lib/database';
import RocketChat from '../lib/rocketchat';
import {
ATTEMPTS_KEY,
CHANGE_PASSCODE_EMITTER,
Expand All @@ -21,6 +21,8 @@ import EventEmitter from './events';
import { isIOS } from './deviceInfo';

export const saveLastLocalAuthenticationSession = async (server: string, serverRecord?: TServerModel): Promise<void> => {
const timesync: number = (await RocketChat.getServerTimeSync(server)) ?? 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's awkward to call this request again here.
localAuthenticate() already calls it.


const serversDB = database.servers;
const serversCollection = serversDB.get('servers');
await serversDB.write(async () => {
Expand All @@ -29,7 +31,7 @@ export const saveLastLocalAuthenticationSession = async (server: string, serverR
serverRecord = (await serversCollection.find(server)) as TServerModel;
}
await serverRecord.update(record => {
record.lastLocalAuthenticatedSession = new Date();
record.lastLocalAuthenticatedSession = new Date(timesync);
});
} catch (e) {
// Do nothing
Expand Down Expand Up @@ -128,11 +130,19 @@ export const localAuthenticate = async (server: string): Promise<void> => {

// `checkHasPasscode` results newPasscode = true if a passcode has been set
if (!result?.newPasscode) {
// Get time from server
const timesync: number | null = await RocketChat.getServerTimeSync(server);

// diff to last authenticated session
const diffToLastSession = moment().diff(serverRecord?.lastLocalAuthenticatedSession, 'seconds');
let diffToLastSession = -1;
if (timesync) {
diffToLastSession = Math.round((timesync - serverRecord?.lastLocalAuthenticatedSession.getTime()) / 1000);
}

// if last authenticated session is older than configured auto lock time, authentication is required
if (diffToLastSession >= serverRecord.autoLockTime!) {
// check if diffToLastSession is negative
// check if timesync is truly, if isn't will show always the modal
if (!timesync || diffToLastSession < 0 || (serverRecord?.autoLockTime && diffToLastSession >= serverRecord.autoLockTime)) {
// set isLocalAuthenticated to false
store.dispatch(setLocalAuthenticated(false));

Expand Down