-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
72 lines (67 loc) · 2.29 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import React, { useEffect } from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import db, { initDatabase } from './db/init';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { AuthProvider } from './providers/AuthContext';
import LoginScreen from './components/login';
import RegistrationScreen from './components/register';
import DashboardScreen from './components/dashboard';
import ProductsScreen from './components/products.js';
const Stack = createStackNavigator();
export default function App() {
useEffect(() => {
initDatabase();
}, []);
const checkSession = async ({ navigation, redirectToDashboard }) => {
try {
const session = await AsyncStorage.getItem('session');
if (session) {
if (redirectToDashboard) {
navigation.reset({
index: 0,
routes: [{ name: 'Dashboard' }],
});
}
} else {
if (!redirectToDashboard) {
navigation.navigate('Login');
}
}
} catch (error) {
console.error('Błąd podczas sprawdzania sesji:', error);
}
};
return (
<AuthProvider>
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
name="Login"
component={LoginScreen}
options={{ title: 'Logowanie' }}
initialParams={{ checkSession: checkSession, redirectToDashboard: true }}
/>
<Stack.Screen
name="Registration"
component={RegistrationScreen}
options={{ title: 'Rejestracja' }}
initialParams={{ checkSession: checkSession, redirectToDashboard: true }}
/>
<Stack.Screen
name="Dashboard"
component={DashboardScreen}
options={{ title: 'Moja lista zakupów' }}
initialParams={{ checkSession: checkSession, redirectToDashboard: false }}
/>
<Stack.Screen
name="Products"
component={ProductsScreen}
options={{ title: 'Moje produkty' }}
initialParams={{ checkSession: checkSession, redirectToDashboard: false }}
/>
</Stack.Navigator>
</NavigationContainer>
</AuthProvider>
);
}