React Native Expo + Firebase如何激活AppCheck

q35jwt9p  于 2023-06-24  发布在  React
关注(0)|答案(1)|浏览(170)

我正在使用Expo + Firebase SDK构建一个应用程序,并希望立即实现AppCheck。如何将其应用到App中?如何为IOS(DeviceCheck)和Android(Play Integrity)创建AppChestCustomProvider?我如何获得tokenFromServer和expireTimeMillis?有没有一种完全不同的方法来解决这个问题?

import { initializeAppCheck, ReCaptchaV3Provider, CustomProvider } from "firebase/app-check"

    const appCheckCustomProvider = new CustomProvider({
     getToken: () => {
    return new Promise((resolve, _reject) => {
      // TODO: Logic to exchange proof of authenticity for an App Check token and
      // expiration time.

      // ...

      const appCheckToken = {
        token: tokenFromServer,
        expireTimeMillis: expirationFromServer * 1000
      };

      resolve(appCheckToken);
    });
  }
});

// Pass your reCAPTCHA v3 site key (public key) to activate(). Make sure this
// key is the counterpart to the secret key you set in the Firebase console.
const appCheck = initializeAppCheck(app, {
  provider: appCheckCustomProvider,

  // for web: new ReCaptchaV3Provider('6LdOHjAlAAAAAAuELCh4n53HGv01THoZVFEJTGO5'),
  // Optional argument. If true, the SDK automatically refreshes App Check
  // tokens as needed.
  isTokenAutoRefreshEnabled: true
});
bjp0bcyl

bjp0bcyl1#

您需要使用react-native-firebase才能在expo中使用AppCheck。您可以在这里找到文档:https://rnfirebase.io/app-check/usage
Expo托管流的一个重要注意事项是,您需要有一个自定义的开发客户端设置,以便您可以添加该包所需的配置插件。您需要添加的配置插件示例可以在这里找到:https://rnfirebase.io/app-check/usage
在遵循这些步骤之后,您应该有一个工作实现。请注意,对于Web,您需要使用firebase-js SDK。
然后,您可以拥有设备特定的设置文件。所以AppCheck.tsx:

export function setupAppCheck(_app: FirebaseApp) {
  const appCheckProvider = firebase.appCheck().newReactNativeFirebaseAppCheckProvider()
  appCheckProvider.configure({
    android: {
      provider: __DEV__ ? 'debug' : 'playIntegrity',
      debugToken: 'YOUR_DEBUG_TOKEN',
    },
    apple: {
      provider: __DEV__ ? 'debug' : 'appAttestWithDeviceCheckFallback',
      debugToken: 'YOUR_DEBUG_TOKEN',
    },
  })
  firebase.appCheck().initializeAppCheck({ provider: appCheckProvider, isTokenAutoRefreshEnabled: true })
}

和AppCheck.web.ts

export function setupAppCheck(app: FirebaseApp) {
  // This will set the debug token for using AppCheck during development (localhost and dev clients)
  if (__DEV__) {
    //@ts-expect-error
    self.FIREBASE_APPCHECK_DEBUG_TOKEN = 'YOUR_DEBUG_TOKEN'
  }
  appCheck = initializeAppCheck(app, {
    provider: new ReCaptchaV3Provider('YOUR_SITE KEY'),
    isTokenAutoRefreshEnabled: true,
  })
}

如果您想添加更多的信息,请告诉我。

相关问题