Browse Source

Merge branch 'main' of https://github.com/IGSD-UoW/WPD-MobileApp into main

master
analuizaff 3 years ago
parent
commit
86e9401e79
  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 navigationTheme from "./app/navigation/navigationTheme";
import "./app/config/globals.js";
import openDatabase from "./app/database/database-connection";
import { AppLoading } from "expo";
import initDatabase from "./app/database/database-init";
import FlashMessage from "react-native-flash-message";
import AppNavigator from "./app/navigation/AppNavigator";
import EventLocationProvider from "./app/context/EventLocationContext";
@ -26,10 +24,16 @@ export default function App() {
const netInfo = useNetInfo();
useEffect(() => {
user && authStorage.setUser(user)
}, [user])
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();
if (user) setUser(user);
};
if (!isReady && netInfo.isInternetReachable) {
@ -37,8 +41,6 @@ export default function App() {
<AppLoading startAsync={restoreUser} onFinish={() => setIsReady(true)} />
);
} else if (netInfo.isInternetReachable) {
global.userDataBase = openDatabase();
initDatabase(global.userDataBase);
global.formsSockets = useFiltering(global.location);
return (

29
src/app/auth/storage.js

@ -1,11 +1,11 @@
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) => {
try {
await SecureStore.setItemAsync(key, authToken);
await SecureStore.setItemAsync(tokenKey, authToken);
} catch (error) {
console.log("Error while storing authToken");
}
@ -13,7 +13,7 @@ const setToken = async (authToken) => {
const getToken = async () => {
try {
return await SecureStore.getItemAsync(key);
return await SecureStore.getItemAsync(tokenKey);
} catch (error) {
console.log("Error while gettig authToken");
}
@ -21,15 +21,28 @@ const getToken = async () => {
const removeToken = async () => {
try {
await SecureStore.deleteItemAsync(key);
await SecureStore.deleteItemAsync(tokenKey);
} catch (e) {
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 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