Skip to content

Commit

Permalink
fix: Ability to login and logout offline
Browse files Browse the repository at this point in the history
  • Loading branch information
hopetambala committed Apr 3, 2022
1 parent a19aa4b commit ffd54d4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 23 deletions.
34 changes: 24 additions & 10 deletions context/auth.context.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { createContext, useEffect, useState } from 'react';

import { deleteData, getData, storeData } from '../modules/async-storage';
import { getData, storeData } from '../modules/async-storage';
import checkOnlineStatus from '../modules/offline';
import {
retrieveCurrentUserAsyncFunction,
retrieveSignInFunction,
Expand Down Expand Up @@ -47,6 +48,7 @@ export const UserContextProvider = ({ children }) => {
setUser(usr);
storeData(usr, 'currentUser');
storeData(password, 'password');
setError(null);
setIsLoading(false);
return true;
})
Expand All @@ -59,12 +61,17 @@ export const UserContextProvider = ({ children }) => {

const offlineLogin = (enteredCredentials) => {
const { username, password } = enteredCredentials;
const { username: usr, password: pswd } = user; // cached user
const { name: usrname, password: pswd } = user; // cached user

setIsLoading(true);

if (username !== usr && password !== pswd) return false;
if (username !== usrname || password !== pswd) {
setError('signIn.usernamePasswordIncorrect');
setIsLoading(false);
return false;
}

setError(null);
setIsLoading(false);

return true;
Expand All @@ -82,20 +89,27 @@ export const UserContextProvider = ({ children }) => {
try {
const u = await retrieveSignUpFunction(params);
setUser(u);
setError(null);
setIsLoading(false);
} catch (e) {
setIsLoading(false);
setError(e.toString());
}
};

const onLogout = async () => retrieveSignOutFunction()
.then(() => {
setUser(null);
setError(null);
deleteData('currentUser');
return true;
});
const onLogout = async () => {
const connected = await checkOnlineStatus();
if (connected) {
return retrieveSignOutFunction()
.then(() => {
setError(null);
return true;
});
}

setError(null);
return true;
};

return (
<UserContext.Provider
Expand Down
22 changes: 12 additions & 10 deletions domains/Auth/SignIn/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ const SignIn = ({ navigation }) => {
checkLanguage();
}, []);

const handleFailedAttempt = () => {
const handleFailedAttempt = (err) => {
const translatedError = I18n.t(error) || err || error || I18n.t('global.pleaseTryAgain');

Alert.alert(
I18n.t('signIn.unableLogin'),
`${error}`, [
translatedError, [
{ text: 'OK' }
],
{ cancelable: true }
Expand Down Expand Up @@ -108,18 +110,20 @@ const SignIn = ({ navigation }) => {
deleteData('currentUser');
};

const signin = async (connected, enteredValues, actions) => {
if (connected) {
const signin = async (enteredValues, actions) => {
const connected = await checkOnlineStatus();

if (connected === true) {
return onlineLogin(enteredValues).then((status) => {
if (status) {
return handleSignIn(enteredValues, actions.resetForm)
.catch(() => handleFailedAttempt());
.catch((err) => handleFailedAttempt(err));
}
return handleFailedAttempt();
});
}
const offlineStatus = offlineLogin();
if (!offlineStatus) return handleFailedAttempt();
const offlineStatus = offlineLogin(enteredValues);
if (offlineStatus === false) return handleFailedAttempt('Unable to login offline, please check your credentials');
return handleSignIn(enteredValues, actions.resetForm);
};

Expand All @@ -137,9 +141,7 @@ const SignIn = ({ navigation }) => {
<Formik
initialValues={{ username: '', password: '' }}
onSubmit={async (values, actions) => {
await checkOnlineStatus().then((connected) => {
signin(connected, values, actions);
});
await signin(values, actions);
setTimeout(() => {
}, 3000);
}}
Expand Down
3 changes: 2 additions & 1 deletion modules/i18n/english/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"success": "Success!",
"ok": "Ok",
"emptyForm": "Empty Form",
"back": "Back"
"back": "Back",
"pleaseTryAgain": "Please, try again"
},
"signUp": {
"firstName": "First Name",
Expand Down
3 changes: 2 additions & 1 deletion modules/i18n/kreyol/hk.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"success": "Siksè!",
"ok": "Ok",
"emptyForm": "Vide Fòm",
"back":"Tounen"
"back":"Tounen",
"pleaseTryAgain": "Tanpri eseye ankò"
},
"signUp": {
"firstName": "Non",
Expand Down
3 changes: 2 additions & 1 deletion modules/i18n/spanish/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"success":"¡Éxito!",
"ok": "OK",
"emptyForm": "Forma vacía",
"back": "Regresa"
"back": "Regresa",
"pleaseTryAgain": "Inténtalo de nuevo"
},
"signUp": {
"firstName": "Nombre de verdadero",
Expand Down

0 comments on commit ffd54d4

Please sign in to comment.