-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
240 additions
and
33 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import AsyncStorage from '@react-native-community/async-storage'; | ||
import React, { | ||
createContext, | ||
useCallback, | ||
FC, | ||
useState, | ||
useContext, | ||
useEffect, | ||
} from 'react'; | ||
|
||
import api from '../services/api'; | ||
|
||
interface AuthState { | ||
token: string; | ||
user: Record<string, unknown>; | ||
} | ||
|
||
interface SignInCredentials { | ||
email: string; | ||
password: string; | ||
} | ||
|
||
interface AuthContextData { | ||
user: Record<string, unknown>; | ||
loading: boolean; | ||
signIn(credentials: SignInCredentials): Promise<void>; | ||
signOut(): void; | ||
} | ||
|
||
const AuthContext = createContext<AuthContextData>({} as AuthContextData); | ||
|
||
const AuthProvider: FC = ({ children }) => { | ||
const [data, setData] = useState<AuthState>({} as AuthState); | ||
const [loading, setLoading] = useState(true); | ||
|
||
useEffect(() => { | ||
const loadStorageData = async (): Promise<void> => { | ||
const [token, user] = await AsyncStorage.multiGet([ | ||
'@GoBarber:token', | ||
'@GoBarber:user', | ||
]); | ||
|
||
if (token[1] && user[1]) { | ||
setData({ token: token[1], user: JSON.parse(user[1]) }); | ||
} | ||
|
||
setLoading(false); | ||
}; | ||
|
||
loadStorageData(); | ||
}); | ||
|
||
const signIn = useCallback(async ({ email, password }) => { | ||
const response = await api.post('sessions', { | ||
email, | ||
password, | ||
}); | ||
|
||
const { token, user } = response.data; | ||
|
||
await AsyncStorage.multiSet([ | ||
['@GoBarber:token', token], | ||
['@GoBarber:user', JSON.stringify(user)], | ||
]); | ||
|
||
setData({ token, user }); | ||
}, []); | ||
|
||
const signOut = useCallback(async () => { | ||
await AsyncStorage.multiRemove(['@GoBarber:token', '@GoBarber:user']); | ||
|
||
setData({} as AuthState); | ||
}, []); | ||
|
||
return ( | ||
<AuthContext.Provider value={{ user: data.user, loading, signIn, signOut }}> | ||
{children} | ||
</AuthContext.Provider> | ||
); | ||
}; | ||
|
||
const useAuth = (): AuthContextData => { | ||
const context = useContext(AuthContext); | ||
|
||
if (!context) { | ||
throw new Error('useAuth must be used within an AuthProvider'); | ||
} | ||
|
||
return context; | ||
}; | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import React, { FC } from 'react'; | ||
|
||
import { AuthProvider } from './auth'; | ||
|
||
const AppProvider: FC = ({ children }) => ( | ||
<AuthProvider>{children}</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import React, { FC } from 'react'; | ||
import { View, Button } from 'react-native'; | ||
import { useAuth } from '../../hooks/auth'; | ||
|
||
const Dashboard: FC = () => { | ||
const { signOut } = useAuth(); | ||
return ( | ||
<View style={{ flex: 1, justifyContent: 'center' }}> | ||
<Button title="Sair" onPress={signOut} /> | ||
</View> | ||
); | ||
}; | ||
|
||
export default Dashboard; |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { createStackNavigator } from '@react-navigation/stack'; | ||
import React, { FC } from 'react'; | ||
import Dashboard from '../pages/Dashboard'; | ||
|
||
const App = createStackNavigator(); | ||
|
||
const AppRoutes: FC = () => ( | ||
<App.Navigator | ||
screenOptions={{ | ||
headerShown: false, | ||
cardStyle: { backgroundColor: '#312e38' }, | ||
}} | ||
> | ||
<App.Screen name="Dashboard" component={Dashboard} /> | ||
</App.Navigator> | ||
); | ||
|
||
export default AppRoutes; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { createStackNavigator } from '@react-navigation/stack'; | ||
import React, { FC } from 'react'; | ||
import SignIn from '../pages/SignIn'; | ||
import SignUp from '../pages/SignUp'; | ||
|
||
const Auth = createStackNavigator(); | ||
|
||
const AuthRoutes: FC = () => ( | ||
<Auth.Navigator | ||
screenOptions={{ | ||
headerShown: false, | ||
cardStyle: { backgroundColor: '#312e38' } | ||
}} | ||
> | ||
<Auth.Screen name="SignIn" component={SignIn} /> | ||
<Auth.Screen name="SignUp" component={SignUp} /> | ||
</Auth.Navigator> | ||
) | ||
|
||
export default AuthRoutes; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,23 @@ | ||
import { createStackNavigator } from '@react-navigation/stack'; | ||
import React, { FC } from 'react'; | ||
import SignIn from '../pages/SignIn'; | ||
import SignUp from '../pages/SignUp'; | ||
import { View, ActivityIndicator } from 'react-native'; | ||
|
||
const Auth = createStackNavigator(); | ||
import AuthRoutes from './auth.routes'; | ||
import AppRoutes from './app.routes'; | ||
|
||
const Routes: FC = () => ( | ||
<Auth.Navigator | ||
screenOptions={{ | ||
headerShown: false, | ||
cardStyle: { backgroundColor: '#312e38' } | ||
}} | ||
> | ||
<Auth.Screen name="SignIn" component={SignIn} /> | ||
<Auth.Screen name="SignUp" component={SignUp} /> | ||
</Auth.Navigator> | ||
) | ||
import { useAuth } from '../hooks/auth'; | ||
|
||
const Routes: 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; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import axios from 'axios'; | ||
|
||
const api = axios.create({ | ||
baseURL: 'http://192.168.1.20:3333', | ||
}); | ||
|
||
export default api; |
Oops, something went wrong.