@cori-risi/cori.data.api / ApiContextProvider
ApiContextProvider()
ApiContextProvider(
props
):Element
This component provides the API/data service context (ApiContext
) to a React application. The following example assumes that the App
component has been configured by the AmplifyContextProvider
to allow for authentication with AWS Cognito, but this provider can also be used to setup an ApiContext with no authentication (by only using the baseURL
param/prop and disregarding the other props):
import {
,
withAuthenticator,
useAuthenticator, useTheme, Heading
UseAuthenticatorfrom '@aws-amplify/ui-react';
} import { fetchAuthSession } from "@aws-amplify/auth";
import { getCurrentUser } from "@aws-amplify/auth/cognito";
import { AmplifyContext, ApiContextProvider } from "@cori-risi/cori.data.api";
// ...
const App = () => {
const amplifyContext = useContext(AmplifyContext);
const authenticator: UseAuthenticator = useAuthenticator();
// ...
return (
<ApiContextProvider baseURL={import.meta.env.VITE_CORI_DATA_API}
={fetchAuthSession}
fetchAuthSession={getCurrentUser}
getCurrentUser={authenticator.signOut} >
signOut<AppComponentsThatNeedAccessToAPI />
</ApiContextProvider>
;
)
}
export default withAuthenticator(App, {
...
; })
Parameters
• props
• props.baseURL?: string
Base URL for the RESTful API endpoint, e.g., https://cori-data-api.ruralinnovation.us.
• props.children?: ReactElement
<any
, any
> | ReactElement
<any
, any
>[]
• props.fetchAuthSession?: Function
An optional function from the Amplify Auth package to start an authenticated session
• props.getCurrentUser?: Function
An optional function from the Amplify Cognito package to fetch the current authenticated user (if any)
• props.signOut?: Function
An optional function that is one of many destructured props contained in the Amplify authenticator context (returned by the useAuthenticator() hoook), used to sign out the current user.
Returns
Element