Created
October 20, 2021 11:22
-
-
Save alanfoandrade/560fbde151f7426342561ed67bdf2f9d to your computer and use it in GitHub Desktop.
This file contains 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
//src/hooks/auth.tsx | |
import React, { | |
useEffect, | |
createContext, | |
useCallback, | |
useState, | |
useContext, | |
} from 'react'; | |
import AsyncStorage from '@react-native-community/async-storage'; | |
import api from '../services/api'; | |
interface IUser { | |
id: string; | |
name: string; | |
email: string; | |
avatar_url: string; | |
} | |
interface IAuthState { | |
token: string; | |
user: IUser; | |
} | |
interface ISignInCredentials { | |
email: string; | |
password: string; | |
} | |
interface IAuthContextData { | |
user: IUser; | |
loading: boolean; | |
signIn(credentials: ISignInCredentials): Promise<void>; | |
signOut(): void; | |
updateUser(user: IUser): Promise<void>; | |
} | |
const AuthContext = createContext<IAuthContextData>({} as IAuthContextData); | |
const AuthProvider: React.FC = ({ children }) => { | |
const [data, setData] = useState<IAuthState>({} as IAuthState); | |
const [loading, setLoading] = useState(true); | |
useEffect(() => { | |
async function loadStoragedData(): Promise<void> { | |
const [token, user] = await AsyncStorage.multiGet([ | |
'@SEUAPP:token', | |
'@SEUAPP:user', | |
]); | |
if (token[1] && user[1]) { | |
api.defaults.headers.authorization = `Bearer ${token[1]}`; | |
setData({ token: token[1], user: JSON.parse(user[1]) }); | |
} | |
setLoading(false); | |
} | |
loadStoragedData(); | |
}, []); | |
const signIn = useCallback(async ({ email, password }) => { | |
const response = await api.post('sessions', { | |
email, | |
password, | |
}); | |
const { token, user } = response.data; | |
await AsyncStorage.multiSet([ | |
['@SEUAPP:token', token], | |
['@SEUAPP:user', JSON.stringify(user)], | |
]); | |
api.defaults.headers.authorization = `Bearer ${token}`; | |
setData({ token, user }); | |
}, []); | |
const signOut = useCallback(async () => { | |
await AsyncStorage.multiRemove(['@SEUAPP:token', '@SEUAPP:user']); | |
setData({} as IAuthState); | |
}, []); | |
const updateUser = useCallback(async (user: IUser) => { | |
AsyncStorage.setItem('@GoBarber:user', JSON.stringify(user)); | |
setData((prev) => ({ | |
token: prev.token, | |
user, | |
})); | |
}, []); | |
return ( | |
<AuthContext.Provider | |
value={{ user: data.user, signIn, signOut, loading, updateUser }} | |
> | |
{children} | |
</AuthContext.Provider> | |
); | |
}; | |
const useAuth = (): IAuthContextData => useContext(AuthContext); | |
export { AuthProvider, useAuth }; |
This file contains 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
//src/hooks/index.tsx | |
import React from 'react'; | |
import { AuthProvider } from './auth'; | |
const AppProvider: React.FC = ({ children }) => ( | |
<AuthProvider> | |
seus outros contextos... | |
</AuthProvider> | |
); | |
export default AppProvider; |
This file contains 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
//src/routes/index.tsx | |
import React from 'react'; | |
import { ActivityIndicator, View } from 'react-native'; | |
import AuthRoutes from './auth.routes'; | |
import AppRoutes from './app.routes'; | |
import { useAuth } from '../hooks/auth'; | |
const Routes: React.FC = () => { | |
const { user, loading } = useAuth(); | |
if (loading) { | |
return ( | |
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> | |
<ActivityIndicator size="large" color="#999" /> | |
</View> | |
); | |
} | |
return user ? <AppRoutes /> : <AuthRoutes />; | |
}; | |
export default Routes; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment