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);
|
2020-04-15 20:32:38 +00:00
|
|
|
// if (diffToLastSession >= 5) {
|
|
|
|
// const supported = await LocalAuthentication.supportedAuthenticationTypesAsync()
|
|
|
|
// console.log('localAuthenticate -> supported', supported);
|
|
|
|
// const authResult = await LocalAuthentication.authenticateAsync();
|
|
|
|
// if (authResult?.success) {
|
|
|
|
// await saveLastLocalAuthenticationSession(server);
|
|
|
|
// }
|
|
|
|
// return Promise.resolve(authResult?.success);
|
|
|
|
// } else {
|
|
|
|
// await saveLastLocalAuthenticationSession(server);
|
|
|
|
// }
|
2020-04-14 14:07:51 +00:00
|
|
|
return Promise.resolve(true);
|
2020-04-14 13:07:13 +00:00
|
|
|
};
|