Last active
June 25, 2022 20:40
-
-
Save faustoct1/7a4ecc3ab0843ae934b013f82b4834b8 to your computer and use it in GitHub Desktop.
Autenticação + React Navigation em 1 minuto
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Esse é o entry point default do react-native App.js. Ele contém um AuthProvider que detecta sempre | |
que o usuário estiver logado/deslogado para exibir a view correta. Views contém as views do usuário | |
logado/deslogado. | |
*/ | |
import React from 'react' | |
import {AuthProvider} from './AuthCtx' | |
import Views from './Views.js' | |
export default App = () => ( | |
<AuthProvider> | |
<Views /> | |
</AuthProvider> | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Essa view será mostrado por 3 segundos quando o setTimeout executar o usuário estará logado | |
e outra view será exibida. Você pode usar aqui o useAuth do context para determinar se o | |
login do usuário foi feito ou não (e fazer a autenticação como preferir). | |
*/ | |
import React from 'react'; | |
import { View, Text } from 'react-native'; | |
export default Auth = (props) => { | |
return <View style={{flex:1,justifyContent:'center',alignItems:'center'}}><Text style={{color:'#000'}}>Login em 3 segundos</Text></View> | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Vamos emular um login no setTimeout no useEffect vc pode substituir esse código pelo seu código | |
que verifica login, por exemplo no AsyncStorage ou outro mecânismo que preferor. | |
*/ | |
import React, {useContext,createContext,useState, useEffect} from 'react' | |
const AuthCtx = createContext() | |
const AuthProvider = (props) => { | |
const [loggedIn,setLoggedIn] = useState(false) | |
return <AuthCtx.Provider value={{loggedIn,setLoggedIn}}>{props.children}</AuthCtx.Provider> | |
} | |
useEffect(()=>{ | |
setTimeout(()=>{ | |
setLoggedIn(true) | |
},3000) | |
return () => {} | |
},[]) | |
const useAuth = () => { | |
const context = useContext(AuthCtx) | |
const {loggedIn,setLoggedIn} = context | |
return {loggedIn,setLoggedIn} | |
} | |
export {AuthCtx,AuthProvider,useAuth} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Tabs será mostrado somente quando o usuário estiver logado. | |
*/ | |
import React from 'react' | |
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs' | |
import { View, Text } from 'react-native' | |
const Tab = createBottomTabNavigator() | |
const Tab1 = (props) => <View style={{flex:1,justifyContent:'center',alignItems:'center'}}><Text style={{color:'#000'}}>tab1</Text></View> | |
const Tab2 = (props) => <View style={{flex:1,justifyContent:'center',alignItems:'center'}}><Text style={{color:'#000'}}>tab2</Text></View> | |
const Tab3 = (props) => <View style={{flex:1,justifyContent:'center',alignItems:'center'}}><Text style={{color:'#000'}}>tab3</Text></View> | |
export default Tabs = (props) => { | |
return ( | |
<Tab.Navigator> | |
<Tab.Screen name="Tab1" component={Tab1} /> | |
<Tab.Screen name="Tab2" component={Tab2} /> | |
<Tab.Screen name="Tab3" component={Tab3} /> | |
</Tab.Navigator> | |
) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Views é o componente responsável por gerenciar as views do react-navigation. | |
Ele renderiza a view correta se o usuário estiver ou não logado. | |
*/ | |
import React from 'react' | |
import { NavigationContainer } from '@react-navigation/native' | |
import { createNativeStackNavigator } from '@react-navigation/native-stack' | |
const Stack = createNativeStackNavigator(); | |
import Tabs from './Tabs' | |
import Auth from './Auth' | |
import {useAuth} from './AuthCtx' | |
export default Views = () => { | |
const {loggedIn} = useAuth() | |
const navigationRef = React.useRef(null); | |
return ( | |
<NavigationContainer ref={navigationRef}> | |
{ | |
loggedIn ? | |
<Stack.Navigator> | |
<Stack.Group> | |
<Stack.Screen name="Tabs" component={Tabs} options={{ headerShown: false }}/> | |
</Stack.Group> | |
</Stack.Navigator> | |
: | |
<Stack.Navigator> | |
<Stack.Group> | |
<Stack.Screen name="Auth" component={Auth} options={{ headerShown: false }}/> | |
</Stack.Group> | |
</Stack.Navigator> | |
} | |
</NavigationContainer> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment