From ffd54d40bf0cad8813fd0359bf244cc0524ff93a Mon Sep 17 00:00:00 2001 From: Hope Tambala Date: Sun, 3 Apr 2022 17:09:10 -0400 Subject: [PATCH] fix: Ability to login and logout offline --- context/auth.context.js | 34 ++++++++++++++++++++++++---------- domains/Auth/SignIn/index.js | 22 ++++++++++++---------- modules/i18n/english/en.json | 3 ++- modules/i18n/kreyol/hk.json | 3 ++- modules/i18n/spanish/es.json | 3 ++- 5 files changed, 42 insertions(+), 23 deletions(-) diff --git a/context/auth.context.js b/context/auth.context.js index bca4a1114..e58588317 100644 --- a/context/auth.context.js +++ b/context/auth.context.js @@ -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, @@ -47,6 +48,7 @@ export const UserContextProvider = ({ children }) => { setUser(usr); storeData(usr, 'currentUser'); storeData(password, 'password'); + setError(null); setIsLoading(false); return true; }) @@ -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; @@ -82,6 +89,7 @@ export const UserContextProvider = ({ children }) => { try { const u = await retrieveSignUpFunction(params); setUser(u); + setError(null); setIsLoading(false); } catch (e) { setIsLoading(false); @@ -89,13 +97,19 @@ export const UserContextProvider = ({ children }) => { } }; - 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 ( { 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 } @@ -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); }; @@ -137,9 +141,7 @@ const SignIn = ({ navigation }) => { { - await checkOnlineStatus().then((connected) => { - signin(connected, values, actions); - }); + await signin(values, actions); setTimeout(() => { }, 3000); }} diff --git a/modules/i18n/english/en.json b/modules/i18n/english/en.json index 5a32a0113..dc62d5c16 100644 --- a/modules/i18n/english/en.json +++ b/modules/i18n/english/en.json @@ -15,7 +15,8 @@ "success": "Success!", "ok": "Ok", "emptyForm": "Empty Form", - "back": "Back" + "back": "Back", + "pleaseTryAgain": "Please, try again" }, "signUp": { "firstName": "First Name", diff --git a/modules/i18n/kreyol/hk.json b/modules/i18n/kreyol/hk.json index 389d9f6db..0138598d8 100644 --- a/modules/i18n/kreyol/hk.json +++ b/modules/i18n/kreyol/hk.json @@ -15,7 +15,8 @@ "success": "Siksè!", "ok": "Ok", "emptyForm": "Vide Fòm", - "back":"Tounen" + "back":"Tounen", + "pleaseTryAgain": "Tanpri eseye ankò" }, "signUp": { "firstName": "Non", diff --git a/modules/i18n/spanish/es.json b/modules/i18n/spanish/es.json index dff4d3daf..ae21e1b6f 100644 --- a/modules/i18n/spanish/es.json +++ b/modules/i18n/spanish/es.json @@ -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",