Open29

Firebase

yorifujiyorifuji

firebase.jsonにこういう記述があるので firebase deployするとlintとbuildが実行されているらしい

firebase.json
% cat firebase.json
{
  "functions": {
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint",
      "npm --prefix \"$RESOURCE_DIR\" run build"
    ]
  }
}
yorifujiyorifuji

functionの記述例

index.ts
export const helloWorld = functions.https.onRequest((request, response) => {
  functions.logger.info("Hello logs!", {structuredData: true});
  response.send("Hello from Firebase!");
});

export const helloWorld2 = functions.https.onRequest((request, response) => {
  functions.logger.info("Hello2 logs!", {structuredData: true});
  response.send("Hello2 from Firebase!");
});
yorifujiyorifuji

firebase auth のユーザ作成時と削除時に関数を実行、関数からfirestoreへのアクセスを行う

index.ts
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";

admin.initializeApp();

exports.onCreateUser = functions.auth.user().onCreate((user) => {
  const uid = user.uid;
  const email = user.email;
  const displayName = user.displayName;
  const newUser = {
    uid: uid,
    email: email,
    displayName: displayName,
  };
  return admin.firestore().collection("users").doc(uid).set(newUser);
});

exports.onDeleteUser = functions.auth.user().onDelete((user) => {
  const uid = user.uid;
  return admin.firestore().collection("users").doc(uid).delete();
});
yorifujiyorifuji

https://tech-blog.sgr-ksmt.org/2019/12/31/160623/

yorifujiyorifuji

jestを使ったfirebase.rulesのテスト環境の構築手順

  • jestのインストール

    • npm install -j jest
  • 初期化

    • jest --init
    • firebase/rules-unit-testing を追加
      • npm i -D @firebase/rules-unit-testing
  • テストコード

    • firestore.rules.test.jsなどを作成
  • テスト実行

    • firebase emulators:exec --only firestore "jest --detectOpenHandles"
yorifujiyorifuji
  • firestore.rules のテストを実行

firebase emulators:exec --only firestore "jest --detectOpenHandles"

yorifujiyorifuji
  • firestore のルールだけデプロイする

firebase deploy --only firestore

yorifujiyorifuji
  • homebrew でnodeをインストールすると v15 になる
  • node が v15 だとfunctionsのビルドに失敗する

https://stackoverflow.com/questions/64437656/gcp-cloud-function-error-fetching-storage-source-during-build-deploy

  • 以下の記事を参考に古いバージョンを(グローバルではなく)プロジェクトに追加した

https://qiita.com/funeasy-soft/items/d08e5a7d96d4bf5b79ed

  • homebrew で node の特定バージョンをインストールする方法

https://qiita.com/rutko/items/016ae50018c5ca19ed95

yorifujiyorifuji

firestore default rule

rules_version = '2';

service cloud.firestore {
  match /databases/{database}/documents {

    // This rule allows anyone with your Firestore database reference to view, edit,
    // and delete all data in your Firestore database. It is useful for getting
    // started, but it is configured to expire after 30 days because it
    // leaves your app open to attackers. At that time, all client
    // requests to your Firestore database will be denied.
    //
    // Make sure to write security rules for your app before that time, or else
    // all client requests to your Firestore database will be denied until you Update
    // your rules
    match /{document=**} {
      allow read, write: if request.time < timestamp.date(2023, 10, 14);
    }
  }
}