-
-
Save niksumeiko/d1d4c5a068d304a697207f1e75d8271d 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
// AuthProvider.tsx (this is an abstraction) | |
type AuthProvider = () => { | |
getToken(): Promise<string>; | |
}; | |
export const { | |
useContext, | |
createContextProvider: provideAuth, | |
} = createGenericContext<AuthProvider>(); | |
export function useAuthProvider() { | |
return useContext().value(); | |
} | |
//////// | |
// useAuth.tsx (this is production implementation) | |
export function useAuth() { | |
const { getAccessTokenSilently } = useAuth0(); | |
return { | |
getToken: getAccessTokenSilently, | |
}; | |
} | |
//////// | |
// AppProvider.tsx | |
export const AppProvider() { | |
return ( | |
<DependencyProvider providers={[provideAuth(useAuth)]}> | |
<App /> | |
</DependencyProvier> | |
); | |
} | |
//////// | |
// useTodos.tsx | |
export function useTodos() { | |
const { getToken } = useAuthProvider(); | |
return useQuery({ | |
queryKey: ['todos'], | |
queryFn: async () => { | |
const token = await getToken(); | |
}, | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment