Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add App Check #430

Merged
merged 7 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/reference/modules/index.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 57 additions & 21 deletions docs/reference/modules/sdk.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions docs/use.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [Setup](#setup)
* [Initialize product SDKs and register them with ReactFire](#initialize-product-sdks-and-register-them-with-reactfire)
* [Connect to the Firebase Local Emulator Suite](#connect-to-the-firebase-local-emulator-suite)
* [Set up App Check](#set-up-app-check)
- [Auth](#auth)
* [Display the current signed-in user](#display-the-current-signed-in-user)
* [Only render a component if a user is signed in](#only-render-a-component-if-a-user-is-signed-in)
Expand Down Expand Up @@ -119,6 +120,39 @@ function FirebaseComponents({ children }) {

Learn more about the Local Emulator Suite in the [Firebase docs](https://firebase.google.com/docs/emulator-suite/connect_and_prototype).

### Set up App Check

[App Check](https://firebase.google.com/docs/app-check) helps protect your backend resources from abuse, such as billing fraud and phishing.

```jsx
import { initializeAppCheck, ReCaptchaV3Provider } from "firebase/app-check";
import { useFirebaseApp, AppCheckProvider } from 'reactfire';

// Create your reCAPTCHA v3 site key in the
// "Project Settings > App Check" section of the Firebase console
const APP_CHECK_TOKEN = 'abcdefghijklmnopqrstuvwxy-1234567890abcd';

function FirebaseComponents({ children }) {
const app = useFirebaseApp(); // a parent component contains a `FirebaseAppProvider`

const appCheck = initializeAppCheck(app, {
provider: new ReCaptchaV3Provider(APP_CHECK_TOKEN),
isTokenAutoRefreshEnabled: true
});

// Activate App Check at the top level before any component talks to an App-Check-compatible Firebase service
return (
<AppCheckProvider>
jhuleatt marked this conversation as resolved.
Show resolved Hide resolved
<DatabaseProvider sdk={database}>
<MyCoolApp/>
</DatabaseProvider>
</AppCheckProvider>
);
}
```

See the [App Check setup guide in the Firebase docs](https://firebase.google.com/docs/app-check/web/recaptcha-provider#project-setup) for more detailed instructions.

## Auth

The following samples assume that `FirebaseAppProvider` and `AuthProvider` components exist higher up the component tree.
Expand Down
6 changes: 5 additions & 1 deletion src/sdk.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';

import type { AppCheck } from 'firebase/app-check';
import type { Auth } from 'firebase/auth';
import type { Analytics } from 'firebase/analytics';
import type { Database } from 'firebase/database';
Expand All @@ -13,6 +14,7 @@ import { ObservableStatus, useObservable } from './useObservable';
import { from } from 'rxjs';
import { ReactFireOptions } from '.';

const AppCheckSdkContext = React.createContext<AppCheck | undefined>(undefined);
const AuthSdkContext = React.createContext<Auth | undefined>(undefined);
const AnalyticsSdkContext = React.createContext<Analytics | undefined>(undefined);
const DatabaseSdkContext = React.createContext<Database | undefined>(undefined);
Expand All @@ -21,7 +23,7 @@ const StorageSdkContext = React.createContext<FirebaseStorage | undefined>(undef
const PerformanceSdkContext = React.createContext<FirebasePerformance | undefined>(undefined);
const RemoteConfigSdkContext = React.createContext<RemoteConfig | undefined>(undefined);

type FirebaseSdks = Auth | Analytics | Database | Firestore | FirebasePerformance | FirebaseStorage | RemoteConfig;
type FirebaseSdks = Analytics | AppCheck | Auth | Database | Firestore | FirebasePerformance | FirebaseStorage | RemoteConfig;

function getSdkProvider<Sdk extends FirebaseSdks>(SdkContext: React.Context<Sdk | undefined>) {
return function SdkProvider(props: React.PropsWithChildren<{ sdk: Sdk }>) {
Expand Down Expand Up @@ -81,6 +83,7 @@ function useInitSdk<Sdk extends FirebaseSdks>(
return useObservable<Sdk>(`firebase-sdk:${sdkName}:${firebaseApp.name}`, from(initializeSdk), options);
}

export const AppCheckProvider = getSdkProvider<AppCheck>(AppCheckSdkContext);
export const AuthProvider = getSdkProvider<Auth>(AuthSdkContext);
export const AnalyticsProvider = getSdkProvider<Analytics>(AnalyticsSdkContext);
export const DatabaseProvider = getSdkProvider<Database>(DatabaseSdkContext);
Expand All @@ -89,6 +92,7 @@ export const PerformanceProvider = getSdkProvider<FirebasePerformance>(Performan
export const StorageProvider = getSdkProvider<FirebaseStorage>(StorageSdkContext);
export const RemoteConfigProvider = getSdkProvider<RemoteConfig>(RemoteConfigSdkContext);

export const useAppCheck = () => useSdk<AppCheck>(AppCheckSdkContext);
export const useAuth = () => useSdk<Auth>(AuthSdkContext);
export const useAnalytics = () => useSdk<Analytics>(AnalyticsSdkContext);
export const useDatabase = () => useSdk<Database>(DatabaseSdkContext);
Expand Down