Rocket.Chat.ReactNative/app/utils/localAuthentication.js

48 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-04-14 13:07:13 +00:00
import * as LocalAuthentication from 'expo-local-authentication';
2020-04-14 14:07:51 +00:00
import moment from 'moment';
2020-04-14 13:07:13 +00:00
import database from '../lib/database';
2020-04-14 14:07:51 +00:00
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) => {
2020-04-14 13:07:13 +00:00
const serversDB = database.servers;
const serversCollection = serversDB.collections.get('servers');
let serverRecord;
try {
serverRecord = await serversCollection.find(server);
console.log('localAuthenticate -> serverRecord', serverRecord);
} catch (error) {
return Promise.reject();
}
2020-04-14 14:07:51 +00:00
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);
2020-04-14 13:07:13 +00:00
};