-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
120 lines (110 loc) · 3.76 KB
/
App.tsx
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* Generated with the TypeScript template
* https://github.com/react-native-community/react-native-template-typescript
*
* @format
*/
import React, { useEffect, useState } from 'react';
import {
Text,
HStack,
Center,
Switch,
useColorMode,
NativeBaseProvider,
} from 'native-base';
import * as Keychain from "react-native-keychain";
import { LoginComponent } from './src/components/Login';
import { TabsComponent } from './src/components/Tabs';
import jwtDecode from "jwt-decode";
import { JwtPayload } from "jwt-decode";
// Color Switch Component
function ToggleDarkMode() {
const { colorMode, toggleColorMode } = useColorMode();
return (
<HStack space={2} alignItems="center">
<Text>Dark</Text>
<Switch
isChecked={colorMode === 'light' ? true : false}
onToggle={toggleColorMode}
aria-label={
colorMode === 'light' ? 'switch to dark mode' : 'switch to light mode'
}
/>
<Text>Light</Text>
</HStack>
);
}
const App = () => {
const [credentials, setCredentials] = useState<false | Keychain.UserCredentials>(false);
useEffect(() => {
Keychain.getGenericPassword().then(pass => {
setCredentials(pass)
}).catch(e => console.log(e));
}, [null])
// async () => {
// try {
// const [token, setToken] = useState('');
// credentials = await Keychain.getGenericPassword();
// setyActive(credentials);
// // Retrieve the credentials
// } catch (error) {
// console.log("Keychain couldn't be accessed!", error);
// }
// };
if (credentials) {
const decodedToken: JwtPayload = jwtDecode(credentials.password);
const isExpired = decodedToken.exp! * 1000 < Date.now()
console.log('Credentials successfully loaded for user ' + credentials.username);
if (isExpired) {
return <NativeBaseProvider>
<Center flex={1} px="3">
<LoginComponent />
</Center>
</NativeBaseProvider>
} else
return (
<NativeBaseProvider>
<Center flex={1} px="3">
<TabsComponent token={credentials.password} />
</Center>
</NativeBaseProvider>
);
} else {
console.log('No credentials stored');
return <NativeBaseProvider>
<Center flex={1} px="3">
<LoginComponent />
</Center>
</NativeBaseProvider>
}
// return (
// <NativeBaseProvider>
// <Center
// _dark={{ bg: 'blueGray.900' }}
// _light={{ bg: 'blueGray.50' }}
// px={4}
// flex={1}>
// <VStack space={5} alignItems="center">
// <NativeBaseIcon />
// <Heading size="lg">Welcome to NativeBase</Heading>
// <HStack space={2} alignItems="center">
// <Text>Edit</Text>
// <Code>App.tsx</Code>
// <Text>and save to reload.</Text>
// </HStack>
// <Link href="https://docs.nativebase.io" isExternal>
// <Text color="primary.500" underline fontSize={'xl'}>
// Learn NativeBase
// </Text>
// </Link>
// <ToggleDarkMode />
// </VStack>
// </Center>
// </NativeBaseProvider>
// );
}
export default App;