-
Notifications
You must be signed in to change notification settings - Fork 10
/
App.js
54 lines (46 loc) · 1.31 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
import React, { useState, useEffect } from "react";
import { StyleSheet, Text, View } from "react-native";
import * as SecureStore from "expo-secure-store";
import Auth from "./src/Auth";
import Main from "./src/Main";
import { ID_TOKEN_KEY } from "./config";
const 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}>
{token && user && <Main token={token} user={user} />}
<Auth token={token} onLogin={handleLogin} onLogout={handleLogout} />
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center"
}
});
export default App;