Skip to content

Commit

Permalink
Fix accessToken hook
Browse files Browse the repository at this point in the history
  • Loading branch information
koredefashokun committed Jun 22, 2020
1 parent b59344a commit 9078747
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
1 change: 1 addition & 0 deletions app/App.tsx
Expand Up @@ -184,6 +184,7 @@ const App = () => {
})
});

// Keep the splash screen open until loading becomes true.
return loading ? (
<View>
<ActivityIndicator />
Expand Down
12 changes: 5 additions & 7 deletions app/src/hooks/useAccessToken.ts
Expand Up @@ -8,9 +8,10 @@ const useAccessToken = () => {
React.useEffect(() => {
(async () => {
try {
const savedAccessToken = await AsyncStorage.getItem('accessToken');
if (!savedAccessToken) return setAccessToken(null);
setAccessToken(savedAccessToken);
const savedToken = await AsyncStorage.getItem('accessToken');
setAccessToken(savedToken);
} catch (error) {
setAccessToken(null);
} finally {
setLoading(false);
}
Expand All @@ -25,12 +26,9 @@ const useAccessToken = () => {
AsyncStorage.setItem('accessToken', accessToken);
}, [accessToken]);

const saveAccessToken = (token: string) =>
AsyncStorage.setItem('accessToken', token);

const logOut = () => setAccessToken(null);

return { loading, accessToken, saveAccessToken, logOut };
return { loading, accessToken, setAccessToken, logOut };
};

export default useAccessToken;
4 changes: 2 additions & 2 deletions app/src/screens/VerifyAuthentication.tsx
Expand Up @@ -17,7 +17,7 @@ const VerifyAuthentication = () => {
RouteProp<AuthStackParamList, 'VerifyAuthentication'>
>();
const { navigate } = useNavigation();
const { saveAccessToken } = useAccessToken();
const { setAccessToken } = useAccessToken();
const [code, setCode] = React.useState('');
const [
{ data, fetching },
Expand All @@ -27,7 +27,7 @@ const VerifyAuthentication = () => {

React.useEffect(() => {
if (data?.verifyAuthentication) {
saveAccessToken(data.verifyAuthentication);
setAccessToken(data.verifyAuthentication);
navigate('Main');
}
}, [data]);
Expand Down

0 comments on commit 9078747

Please sign in to comment.