-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
71 lines (55 loc) · 1.43 KB
/
App.js
File metadata and controls
71 lines (55 loc) · 1.43 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
62
63
64
65
66
67
68
69
70
71
import React, {useMemo, useState, useEffect} from 'react';
import { StyleSheet, Text, View, Button, } from 'react-native';
import jwtDecode from 'jwt-decode'
import { Provider as PaperProvider } from 'react-native-paper';
import { setTokenApi, getTokenApi, removeTokenApi } from './src/api/token';
import AuthContext from './src/context/AuthContext';
import AuthScreen from './src/screens/Auth'
import AppNavigation from './src/navigation/AppNavigation';
export default function App() {
const [auth, setAuth] = useState(undefined)
useEffect(() => {
(async() => {
const token = await getTokenApi();
if(token){
console.log('estoy logueado')
// console.log(token)
console.log(jwtDecode(token).id);
setAuth({
token,
idUser: jwtDecode(token).id,
});
}else{
setAuth(null)
}
}) ();
}, [])
const login = (user) => {
console.log('LOGIN APP.JS')
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>
{ auth ? <AppNavigation />: <AuthScreen />}
</PaperProvider>
</AuthContext.Provider>
);
}
const styles = StyleSheet.create({});