{"id":18150,"date":"2022-10-09T11:20:22","date_gmt":"2022-10-09T02:20:22","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=18150"},"modified":"2023-05-27T17:42:15","modified_gmt":"2023-05-27T08:42:15","slug":"%e3%80%90firebase%e3%80%91%e3%80%8ccloud-function%e3%80%8d%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=18150","title":{"rendered":"\u3010Firebase\u3011\u300cCloud Function\u300d\u3068\u300cHasura\u300d\u306e\u9023\u643a"},"content":{"rendered":"<h2>\u524d\u63d0\u6761\u4ef6<\/h2>\n<ul>\n<li>\u4e8b\u524d\u306b\u300cfirebase-tools\u300d\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3084\u3001\u30ed\u30b0\u30a4\u30f3\u304c\u5fc5\u8981\u3067\u3059\u3002<\/li>\n<li>\u4e8b\u524d\u306bfirebase\u3067\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3057\u3066\u3044\u308b\u3053\u3068\u3002<\/li>\n<li>\u8a72\u5f53\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u304cBlaz\u30d7\u30e9\u30f3\u306b\u5909\u66f4\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3002(Spark\u30d7\u30e9\u30f3\u3067\u306f\u3060\u3081\uff09<\/li>\n<li>Hasura\u3092\u69cb\u7bc9\u3057\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002<\/li>\n<\/ul>\n<h2 class=\"title-copy\">Cloud Function<\/h2>\n<h2>\u6599\u91d1\u5236\u5ea6\u3001\u30d7\u30e9\u30f3<\/h2>\n<p>\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3054\u3068\u306b\u30d7\u30e9\u30f3\u3092\u8a2d\u5b9a\u3067\u304d\u307e\u3059\u3002\u3044\u3064\u3067\u3082\u7c21\u5358\u306b\u623b\u305b\u308b\u306e\u3067\u5b66\u7fd2\u306e\u305f\u3081\u306b\u4f7f\u3063\u3066\u3044\u308b\u5834\u5408\u306f\u5b66\u7fd2\u5f8c\u306f\u3059\u3050\u306bSpark\u306b\u623b\u3057\u3066\u304a\u304f\u3068\u826f\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<h3>\u30d7\u30e9\u30f3<\/h3>\n<h4>Spark<\/h4>\n<p>\u7121\u6599\u30d7\u30e9\u30f3\u3067\u3059\u3002<\/p>\n<h4>Blaze<\/h4>\n<p>\u5f93\u91cf\u5236\u306e\u6709\u6599\u30d7\u30e9\u30f3\u3067\u3059\u3002<\/p>\n<h2>Cloud Function\u306e\u69cb\u7bc9<\/h2>\n<pre class=\"lang:default decode:true\">firebase init<\/pre>\n<h3>Which Firebase features do you want to set up for this directory? Press Space to select features, then Enter to confirm your choices.<\/h3>\n<p>\u300cFunctions: Configure a Cloud Functions directory and its files\u300d\u3092\u9078\u629e\u3059\u308b\u3002<\/p>\n<h3>Please select an option:<\/h3>\n<p>\u300cUse an existing project\u300d\u3092\u9078\u629e\u3059\u308b\u3002<\/p>\n<h3>Select a default Firebase project for this directory:<\/h3>\n<p>firebase\u3067\u4f5c\u3063\u305f\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u9078\u629e\u3057\u307e\u3059\u3002<\/p>\n<h3>What language would you like to use to write Cloud Functions?<\/h3>\n<p>JS or TS\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067\u4f7f\u3063\u3066\u3044\u308b\u65b9\u3092\u9078\u3073\u307e\u3057\u3087\u3046\u3002<\/p>\n<h3>Do you want to use ESLint to catch probable bugs and enforce style?<\/h3>\n<p>ESLint\u3092\u9069\u7528\u3059\u308b\u5834\u5408\u306fy\u3092\u9078\u3073\u307e\u3059\u3002\u8a66\u3059\u3060\u3051\u306a\u3089\u5207\u3063\u3066\u304a\u3044\u305f\u65b9\u304c\u7121\u96e3\u3067\u3059\u3002\uff08\u3088\u304f\u308f\u304b\u3089\u306a\u3044\u30a8\u30e9\u30fc\u304c\u51fa\u308b\u5834\u5408\u304c\u3042\u308b\u306e\u3067\uff09<\/p>\n<h4>Do you want to install dependencies with npm now?<\/h4>\n<p>dependencies\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u306a\u3089y\u3067\u554f\u984c\u306a\u3044\u3067\u3059\u3002<\/p>\n<p>\u4f5c\u696d\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306bfunctions\u3068\u3044\u3046\u30d5\u30a9\u30eb\u30c0\u304c\u4f5c\u6210\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<h2>Hasura\u3068\u306e\u9023\u643a<\/h2>\n<h3>\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u79fb\u52d5<\/h3>\n<pre class=\"lang:default decode:true\">cd functions<\/pre>\n<h3>Hasura\u3068\u306e\u9023\u643a<\/h3>\n<pre class=\"lang:default decode:true\">firebase functions:config:set hasura.url=\"Hasura\u306e\u30a8\u30f3\u30c9\u30dd\u30a4\u30f3\u30c8URL\" hasura.admin_secret=\"Hasura\u306eadmin\u30b7\u30fc\u30af\u30ec\u30c3\u30c8\"<\/pre>\n<h3>index.js<\/h3>\n<pre class=\"lang:default decode:true\">import * as functions from 'firebase-functions'\r\nimport * as admin from 'firebase-admin'\r\n\r\nadmin.initializeApp()\r\n\r\nexport const setCustomClaims = functions.auth.user().onCreate(async (user) =&gt; {\r\n  const customClaims = {\r\n    'https:\/\/hasura.io\/jwt\/claims': {\r\n      'x-hasura-default-role': 'hasura\u3067\u5b9a\u7fa9\u3057\u305f\u30ed\u30fc\u30eb\u540d',\r\n      'x-hasura-allowed-roles': ['hasura\u3067\u5b9a\u7fa9\u3057\u305f\u30ed\u30fc\u30eb\u540d'],\r\n      'x-hasura-user-id': user.uid,\r\n    },\r\n  }\r\n  try {\r\n    await admin.auth().setCustomUserClaims(user.uid, customClaims)\r\n    await admin.firestore().collection('user_meta').doc(user.uid).create({\r\n      refreshTime: admin.firestore.FieldValue.serverTimestamp(),\r\n    })\r\n  } catch (e) {\r\n    console.log(e)\r\n  }\r\n})\r\n<\/pre>\n<h3>Hasura\u3078\u306e\u30ea\u30af\u30a8\u30b9\u30c8\u30d8\u30c3\u30c0\u30fc(customClaims)<\/h3>\n<h4>x-hasura-default-role<\/h4>\n<p>Hasura\u306b\u30ea\u30af\u30a8\u30b9\u30c8\u3059\u308b\u969b\u306e\u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u30ed\u30fc\u30eb\u3001\u30d7\u30ed\u30b0\u30e9\u30e0\u5185\u3067\u4e0a\u66f8\u304d\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n<h4>x-hasura-allowed-roles<\/h4>\n<p>\u5b58\u5728\u3059\u308b\u30ed\u30fc\u30eb\u306e\u4e00\u89a7\u3092\u914d\u5217\u3067\u6307\u5b9a\u3057\u307e\u3059\u3002\u4f7f\u3044\u305f\u3044\u30ed\u30fc\u30eb\u306f\u5168\u3066\u8ffd\u8a18\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<h4>x-hasura-user-id<\/h4>\n<p>\u30ed\u30b0\u30a4\u30f3\u3057\u3066\u3044\u308b\u30e6\u30fc\u30b6\u30fcID\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002firebase\u3068\u9023\u643a\u3059\u308b\u5834\u5408\u306f\u3001firebase\u306e\u30e6\u30fc\u30b6\u30fcID\u306a\u3069\u3092\u6307\u5b9a\u3059\u308b\u3068\u826f\u3044\u3067\u3057\u3087\u3046\u3002Hasura\u306epermissions\u306e\u6a5f\u80fd\u3068\u7d44\u307f\u5408\u308f\u305b\u308c\u3070\u3001\u7279\u5b9a\u306e\u30e6\u30fc\u30b6\u30fc\u3067\u767b\u9332\u3057\u305f\u3082\u306e\u3092\u305d\u306e\u30e6\u30fc\u30b6\u30fc\u3067\u3057\u304b\u95b2\u89a7\u3067\u304d\u306a\u3044\u3068\u3044\u3046\u3088\u3046\u306a\u5b9f\u88c5\u3092\u7c21\u5358\u306b\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<h3>admin.auth().setCustomUserClaims<\/h3>\n<p>firebase\u306bcustomClaims\u4ed8\u304d\u3067\u30e6\u30fc\u30b6\u30fc\u767b\u9332\u3057\u307e\u3059\u3002\u306a\u304a\u3001customClaims\u306e\u53cd\u6620\u306b\u306f\u5c11\u3057\u30e9\u30b0\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h3>await admin.firestore().collection('user_meta').doc(user.uid).create({ xxx })<\/h3>\n<p>firebase\u306efirestore\u306b\u30e6\u30fc\u30b6\u30fc\u306e\u30e1\u30bf\u60c5\u5831\u3092\u66f8\u304d\u8fbc\u3093\u3067\u3044\u307e\u3059\u3002customClaims\u306e\u53cd\u6620\u306b\u30e9\u30b0\u304c\u3042\u308b\u306e\u3067\u305d\u308c\u304c\u7d42\u308f\u3063\u305f\u3053\u3068\u3092\u5b9f\u88c5\u3059\u308b\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306b\u5225\u9014\u901a\u77e5\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3067\u3059\u3002\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306e\u540d\u524d\u3068\u3057\u3066user_meta\u3068\u3044\u3046\u540d\u524d\u3067\u751f\u6210\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h4>refreshTime: admin.firestore.FieldValue.serverTimestamp(),<\/h4>\n<p>refreshTime\u3068\u3044\u3046\u30d5\u30a3\u30fc\u30eb\u30c9\u3092firestore\u306b\u4f5c\u3063\u3066\u3001\u30b5\u30fc\u30d0\u30fc\u6642\u9593\u3092\u683c\u7d0d\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h2>Cloud Functions\u306e\u30c7\u30d7\u30ed\u30a4<\/h2>\n<pre class=\"lang:default decode:true \">firebase deploy --only functions<\/pre>\n<p>\u8868\u793a\u3055\u308c\u305fURL\u306b\u30a2\u30af\u30bb\u30b9\u3057\u305f\u3089\u7121\u4e8b\u30b5\u30a4\u30c8\u3092\u95b2\u89a7\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u524d\u63d0\u6761\u4ef6 \u4e8b\u524d\u306b\u300cfirebase-tools\u300d\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3084\u3001\u30ed\u30b0\u30a4\u30f3\u304c\u5fc5\u8981\u3067\u3059\u3002 \u4e8b\u524d\u306bfirebase\u3067\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3057\u3066\u3044\u308b\u3053\u3068\u3002 \u8a72\u5f53\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u304cBlaz\u30d7\u30e9\u30f3\u306b\u5909\u66f4\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3002(Spark\u30d7\u30e9\u30f3\u3067 [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[151],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/18150"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=18150"}],"version-history":[{"count":13,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/18150\/revisions"}],"predecessor-version":[{"id":20061,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/18150\/revisions\/20061"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=18150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=18150"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=18150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}