diff --git a/src/AppPersistence.ts b/src/AppPersistence.ts index 2ecdb79..5e2e4b2 100644 --- a/src/AppPersistence.ts +++ b/src/AppPersistence.ts @@ -6,7 +6,7 @@ import {Recipe, RecipeGroup, UserInfo} from './dao/RestAPI'; export default class AppPersistence { static async clearOfflineData() { - await AsyncStorage.multiRemove(['offline_userinfo', 'offline_recipes']); + await AsyncStorage.multiRemove(['offline_userinfo', 'offline_recipes', 'offline_recipegroups', 'offline_userinfo']); } static async getRecipeGroupsOffline(): Promise { const recipeGroups = await AsyncStorage.getItem('offline_recipegroups'); @@ -20,9 +20,9 @@ export default class AppPersistence { } await AsyncStorage.setItem('offline_recipegroups', JSON.stringify(recipeGroups)); } - static async getUserInfoOffline(): Promise { + static async getUserInfoOffline(): Promise { const storedUserinfo = await AsyncStorage.getItem('offline_userinfo'); - if (storedUserinfo === null) return []; + if (storedUserinfo === null) return undefined; return JSON.parse(storedUserinfo); } diff --git a/src/dao/RestAPI.ts b/src/dao/RestAPI.ts index bb77892..6fd212b 100644 --- a/src/dao/RestAPI.ts +++ b/src/dao/RestAPI.ts @@ -47,7 +47,7 @@ export interface WeekplanDay { recipes: WeekplanDayRecipeInfo[] } export interface UserInfo { - + email: string; } export interface InstanceInfo { @@ -477,7 +477,10 @@ class RestAPI { private static async offlineGet(apiPath: string) { // Only for offline stuff that is not managed by redux if (apiPath === '/users/self') { - return {data: await AppPersistence.getUserInfoOffline()}; + const userinfo = await AppPersistence.getUserInfoOffline(); + if (userinfo !== undefined) { + return {data: userinfo}; + } } } } diff --git a/src/redux/features/authSlice.ts b/src/redux/features/authSlice.ts index be21334..c65e0b9 100644 --- a/src/redux/features/authSlice.ts +++ b/src/redux/features/authSlice.ts @@ -1,4 +1,5 @@ import {createSlice} from '@reduxjs/toolkit'; +import AppPersistence from '../../AppPersistence'; export interface AuthState { loggedIn: boolean @@ -21,6 +22,7 @@ export const authSlice = createSlice({ logout: (state) => { state.isLoading = false; state.loggedIn = false; + AppPersistence.clearOfflineData(); }, }, }); diff --git a/src/screens/LoginScreen/SplashScreen.tsx b/src/screens/LoginScreen/SplashScreen.tsx index 290b8bd..f87daa7 100644 --- a/src/screens/LoginScreen/SplashScreen.tsx +++ b/src/screens/LoginScreen/SplashScreen.tsx @@ -55,14 +55,19 @@ export const SplashScreen = () => { // TODO: Proper management of backend url via redux dispatch(changeBackendUrl(await AppPersistence.getBackendURL())); - try { - setStatusText('Logging in...'); - await RestAPI.getUserInfo(); - dispatch(login()); - } catch (e) { - console.error('Login failed'); + setStatusText('Logging in...'); + RestAPI.getUserInfo().then((userinfo) => { + if (userinfo.email) { + console.info('got userinfo, logging in'); + dispatch(login()); + } else { + console.error('invalid userinfo', userinfo); + dispatch(logout()); + } + }).catch((error) => { + console.error('Login failed', error); dispatch(logout()); - } + }); })(); }, []);