Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/AppPersistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RecipeGroup[]> {
const recipeGroups = await AsyncStorage.getItem('offline_recipegroups');
Expand All @@ -20,9 +20,9 @@ export default class AppPersistence {
}
await AsyncStorage.setItem('offline_recipegroups', JSON.stringify(recipeGroups));
}
static async getUserInfoOffline(): Promise<UserInfo> {
static async getUserInfoOffline(): Promise<UserInfo|undefined> {
const storedUserinfo = await AsyncStorage.getItem('offline_userinfo');
if (storedUserinfo === null) return [];
if (storedUserinfo === null) return undefined;
return JSON.parse(storedUserinfo);
}

Expand Down
7 changes: 5 additions & 2 deletions src/dao/RestAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export interface WeekplanDay {
recipes: WeekplanDayRecipeInfo[]
}
export interface UserInfo {

email: string;
}

export interface InstanceInfo {
Expand Down Expand Up @@ -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};
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/redux/features/authSlice.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {createSlice} from '@reduxjs/toolkit';
import AppPersistence from '../../AppPersistence';

export interface AuthState {
loggedIn: boolean
Expand All @@ -21,6 +22,7 @@ export const authSlice = createSlice({
logout: (state) => {
state.isLoading = false;
state.loggedIn = false;
AppPersistence.clearOfflineData();
},
},
});
Expand Down
19 changes: 12 additions & 7 deletions src/screens/LoginScreen/SplashScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
});
})();
}, []);

Expand Down