LoginSignup
1
0

More than 1 year has passed since last update.

Firebase Cloud FunctionsのonCallでCORS(Access-Control-Allow-Origin)エラーになったので手動で対応した話

Posted at

解決方法

アプリから関数を呼び出すfunctions.https.onCallでCloud Functionsを呼び出した際に、以下のようにCORSエラーに遭遇した。
image.png

色々調べてた結果、onCall function returning cors on web and "UNAUTHENTICATED" on mobileに書かれている通り、Google CloudのCloud Functionsの権限設定で以下のような権限を追加したら問題は解決された。
image.png

上記は公式でも触れられているので解決策としてアリだとは思われる。が、その場合、クライアント(ブラウザ)からの全てのクロスドメインアクセスが許可されてしまうので、App Checkでの保護が必須になるだろう(もしApp Checkがない場合、全てのPOSTリクエストが到達してしまいNG)。

※CORSはブラウザからのリクエストでしか発生しないので、バックエンドからのAPI呼び出しはCORSエラーにならず普通に通ってしまう。その意味でもApp Checkによる保護は必須だろう。

うまくいかなかった方法

RuntimeOptions.invokerのallUsersを設定する(publicも同じった)

RuntimeOptions.invokerallUsersに設定する、という方法。実装としては以下のようになる。

export const hogeHoge = functions
	.region('asia-northeast1')
	.runWith({ enforceAppCheck: true, invoker: 'allUsers' })
	.https.onCall(async (data, context) => {
       ...
	});
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0