-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
61 lines (54 loc) · 1.32 KB
/
App.js
File metadata and controls
61 lines (54 loc) · 1.32 KB
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
import "react-native-gesture-handler";
import React, { useState, useMemo, useEffect } from "react";
import { Provider as PaperProvider } from "react-native-paper";
import jwtDecode from "jwt-decode";
import AppNavigation from "./src/Navigation/AppNavigation";
import Auth from "./src/Screens/Auth";
import AuthContext from "./src/Context/AuthContext";
import { setTokenApi, getTokenApi, removeTokenApi } from "./src/Api/Token";
export default function App() {
const [auth, setAuth] = useState(undefined);
useEffect(() => {
(async () => {
const token = await getTokenApi();
if (token) {
setAuth({
token,
idUser: jwtDecode(token).id,
});
} else {
setAuth(null);
}
})();
}, []);
const login = (user) => {
setTokenApi(user.jwt);
setAuth({
token: user.jwt,
idUser: user.user.id,
});
};
const logout = () => {
if (auth) {
removeTokenApi();
setAuth(null);
}
};
const authData = useMemo(
() => ({
auth,
login,
logout,
}),
[auth]
);
if (auth === undefined) return null;
return (
<AuthContext.Provider value={authData}>
<PaperProvider>
<AppNavigation />
{/* {auth ? <AppNavigation /> : <Auth />} */}
</PaperProvider>
</AuthContext.Provider>
);
}