-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
56 lines (49 loc) · 1.43 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
import React, { useState, useEffect } from "react";
import { StyleSheet, View, KeyboardAvoidingView } from "react-native";
import * as SecureStore from "expo-secure-store";
import { ID_TOKEN_KEY } from "./config";
import { Main, Auth } from "./src";
export default function App() {
const [token, setToken] = useState(null);
const [user, setUser] = useState(null);
useEffect(() => {
handleLogin();
}, []);
const handleLogin = (isNewUser = false) => {
SecureStore.getItemAsync(ID_TOKEN_KEY).then((session) => {
if (session) {
const sessionObj = JSON.parse(session);
const { exp, token, id, name } = sessionObj;
if (exp > Math.floor(new Date().getTime() / 1000)) {
setToken(token);
setUser({ id, name, isNewUser });
} else {
handleLogout();
}
}
});
};
const handleLogout = () => {
SecureStore.deleteItemAsync(ID_TOKEN_KEY);
setToken(null);
};
return (
<View style={styles.container}>
<KeyboardAvoidingView
style={{ flex: 1, padding: 100 }}
behavior={"heigth"}
>
{token && user && <Main token={token} user={user} />}
<Auth token={token} onLogin={handleLogin} onLogout={handleLogout} />
</KeyboardAvoidingView>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
});