Browse Source

Fixing bug while gatting user data from secure storage

master
GabrielTrettel 3 years ago
parent
commit
15b4e21108
  1. 16
      src/App.js
  2. 29
      src/app/auth/storage.js

16
src/App.js

@ -1,12 +1,10 @@
import React, { useState } from "react";
import React, { useState, useEffect} from "react";
import { NavigationContainer } from "@react-navigation/native"; import { NavigationContainer } from "@react-navigation/native";
import navigationTheme from "./app/navigation/navigationTheme"; import navigationTheme from "./app/navigation/navigationTheme";
import "./app/config/globals.js"; import "./app/config/globals.js";
import openDatabase from "./app/database/database-connection";
import { AppLoading } from "expo"; import { AppLoading } from "expo";
import initDatabase from "./app/database/database-init";
import FlashMessage from "react-native-flash-message"; import FlashMessage from "react-native-flash-message";
import AppNavigator from "./app/navigation/AppNavigator"; import AppNavigator from "./app/navigation/AppNavigator";
import EventLocationProvider from "./app/context/EventLocationContext"; import EventLocationProvider from "./app/context/EventLocationContext";
@ -26,10 +24,16 @@ export default function App() {
const netInfo = useNetInfo(); const netInfo = useNetInfo();
useEffect(() => {
user && authStorage.setUser(user)
}, [user])
const restoreUser = async () => { const restoreUser = async () => {
const user = await authStorage.getUser();
const storageUser = await authStorage.getUser();
if (storageUser) setUser(storageUser);
global.location = { lat: -23.623, long: -46.5637 }; // await getLocation(); global.location = { lat: -23.623, long: -46.5637 }; // await getLocation();
if (user) setUser(user);
}; };
if (!isReady && netInfo.isInternetReachable) { if (!isReady && netInfo.isInternetReachable) {
@ -37,8 +41,6 @@ export default function App() {
<AppLoading startAsync={restoreUser} onFinish={() => setIsReady(true)} /> <AppLoading startAsync={restoreUser} onFinish={() => setIsReady(true)} />
); );
} else if (netInfo.isInternetReachable) { } else if (netInfo.isInternetReachable) {
global.userDataBase = openDatabase();
initDatabase(global.userDataBase);
global.formsSockets = useFiltering(global.location); global.formsSockets = useFiltering(global.location);
return ( return (

29
src/app/auth/storage.js

@ -1,11 +1,11 @@
import * as SecureStore from "expo-secure-store"; import * as SecureStore from "expo-secure-store";
import jwtDecode from "jwt-decode";
const key = "authToke";
const tokenKey = "authToken";
const userKey = "userKey";
const setToken = async (authToken) => { const setToken = async (authToken) => {
try { try {
await SecureStore.setItemAsync(key, authToken);
await SecureStore.setItemAsync(tokenKey, authToken);
} catch (error) { } catch (error) {
console.log("Error while storing authToken"); console.log("Error while storing authToken");
} }
@ -13,7 +13,7 @@ const setToken = async (authToken) => {
const getToken = async () => { const getToken = async () => {
try { try {
return await SecureStore.getItemAsync(key);
return await SecureStore.getItemAsync(tokenKey);
} catch (error) { } catch (error) {
console.log("Error while gettig authToken"); console.log("Error while gettig authToken");
} }
@ -21,15 +21,28 @@ const getToken = async () => {
const removeToken = async () => { const removeToken = async () => {
try { try {
await SecureStore.deleteItemAsync(key);
await SecureStore.deleteItemAsync(tokenKey);
} catch (e) { } catch (e) {
console.log("Error while removing authToken"); console.log("Error while removing authToken");
} }
}; };
const setUser = async (userData) => {
try {
await SecureStore.setItemAsync(userKey, JSON.stringify(userData));
} catch (error) {
console.log("Error while storing user data");
}
};
const getUser = async () => { const getUser = async () => {
const token = await getToken();
return token ? jwtDecode(token) : null;
try {
return await SecureStore.getItemAsync(userKey).then((data) => {
return JSON.parse(data);
});
} catch (error) {
console.log("Error while getting user data");
}
}; };
export default { setToken, getUser, removeToken, getToken };
export default { setToken, getUser, removeToken, getToken, setUser };
Loading…
Cancel
Save