Created
March 5, 2020 18:49
-
-
Save bogoslavskiy/de14137aa8cc77a6d221771f3de84ec0 to your computer and use it in GitHub Desktop.
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
import React from 'react'; | |
import { AsyncStorage } from 'react-native'; | |
import UUID from 'uuid-random'; | |
import { useApolloClient } from '@apollo/client'; | |
import { useUnregisterDeviceMutation } from '../graphql/generated'; | |
[...] | |
export const AuthProvider: React.FC = ({ children }) => { | |
const [userState, setUserState] = React.useState<User>(); | |
const [unregisterDeviceMutate] = useUnregisterDeviceMutation(); | |
const client = useApolloClient(); | |
[...] | |
const logout = React.useCallback(async () => { | |
try { | |
const pushToken = await AsyncStorage.getItem('PushToken'); | |
if (pushToken) { | |
await unregisterDeviceMutate({ | |
fetchPolicy: 'no-cache', | |
variables: { token: pushToken } | |
}); | |
await AsyncStorage.removeItem('PushToken'); | |
} | |
await client.resetStore(); | |
await clearUserFromStorage(); | |
setUserState(null); | |
} catch (err) { | |
console.error(err); | |
} | |
}, []); | |
return ( | |
<AuthContext.Provider value={{ user: userState, setUser, login, logout }}> | |
{children} | |
</AuthContext.Provider> | |
); | |
}; | |
[...] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment