Skip to content

Instantly share code, notes, and snippets.

@niksumeiko
Last active April 26, 2024 04:48
Show Gist options
  • Save niksumeiko/d1d4c5a068d304a697207f1e75d8271d to your computer and use it in GitHub Desktop.
Save niksumeiko/d1d4c5a068d304a697207f1e75d8271d to your computer and use it in GitHub Desktop.
// 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