{"id":1135,"date":"2024-04-25T19:55:04","date_gmt":"2024-04-25T10:55:04","guid":{"rendered":"https:\/\/shinke1987.net\/?p=1135"},"modified":"2024-04-26T12:38:25","modified_gmt":"2024-04-26T03:38:25","slug":"react-firestore-authentication%e3%81%ae%e5%9f%ba%e6%9c%ac%e7%9a%84%e3%81%aa%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/shinke1987.net\/?p=1135","title":{"rendered":"React + Firestore + Authentication\u306e\u57fa\u672c\u7684\u306a\u64cd\u4f5c"},"content":{"rendered":"\n<h2 id=\"toc0\" class=\"wp-block-heading\">\u76ee\u7684<\/h2>\n\n\n\n<p>Firestore\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u969b\u306b\u8a8d\u8a3c\u3092\u5fc5\u8981\u3068\u3057\u305f\u3044\u3002<\/p>\n\n\n\n<p>\u8a8d\u8a3c\u306e\u305f\u3081\u306e\u30ed\u30b0\u30a4\u30f3\u30d7\u30ed\u30d0\u30a4\u30c0\u306f\u30e1\u30fc\u30eb\/\u30d1\u30b9\u30ef\u30fc\u30c9\uff08\u30d1\u30b9\u30ef\u30fc\u30c9\u8a8d\u8a3c\uff09\u306e\u307f\u3002<\/p>\n\n\n\n<h2 id=\"toc1\" class=\"wp-block-heading\">\u524d\u63d0<\/h2>\n\n\n\n<p>Firestore\u306f\u30c6\u30b9\u30c8\u30e2\u30fc\u30c9\u3002<\/p>\n\n\n\n<p>Firestore\u306bcollection_name\u3068\u3044\u3046\u540d\u524d\u306e\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u304c\u5b58\u5728\u3059\u308b\u3002<\/p>\n\n\n\n<p>Authentication\u306e\u30ed\u30b0\u30a4\u30f3\u30d7\u30ed\u30d0\u30a4\u30c0\u306f\u30e1\u30fc\u30eb\/\u30d1\u30b9\u30ef\u30fc\u30c9\u306e\u307f\u6709\u52b9\u5316\u3002<\/p>\n\n\n\n<p>\u4e0b\u8a18\u306e\u8a8d\u8a3c\u7528\u30a2\u30ab\u30a6\u30f3\u30c8\u304c\u5b58\u5728\u3059\u308b\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>ID\uff1aaa@aa.com<\/li>\n\n\n\n<li>\u30d1\u30b9\u30ef\u30fc\u30c9\uff1apassword<\/li>\n<\/ul>\n\n\n\n<h2 id=\"toc2\" class=\"wp-block-heading\">Firestore\u306e\u30eb\u30fc\u30eb<\/h2>\n\n\n\n<p>\u8a2d\u5b9a\u3057\u305f\u30eb\u30fc\u30eb\u306f\u516c\u5f0f\u306e\u4f8b\u306b\u3082\u3042\u308b\u4e0b\u8a18\u306e\u901a\u308a\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nrules_version = &#039;2&#039;;\n\nservice cloud.firestore {\n  match \/databases\/{database}\/documents {\n    match \/{document=**} {\n    \tallow read, write: if request.auth != null;\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<h2 id=\"toc3\" class=\"wp-block-heading\">tsx\u30d5\u30a1\u30a4\u30eb\u306e\u5185\u5bb9<\/h2>\n\n\n\n<p>\u203b \u5909\u6570\u540d\u30b0\u30c1\u30e3\u30b0\u30c1\u30e3\u3060\u3051\u3069\u6c17\u306b\u3057\u306a\u3044\u3002<\/p>\n\n\n\n<p>\u203b \u30b3\u30e1\u30f3\u30c8\u30a2\u30a6\u30c8\u3092\u4f7f\u3063\u3066\u8a66\u305b\u3070\u628a\u63e1\u3067\u304d\u308b\u306f\u305a\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nimport React from &quot;react&quot;;\nimport ReactDOM from &quot;react-dom\/client&quot;;\nimport {initializeApp} from &#039;firebase\/app&#039;;\nimport {\n    getFirestore,\n    collection,\n    getDocs,\n    deleteDoc,\n    doc,\n    setDoc,\n    updateDoc,\n} from &#039;firebase\/firestore&#039;;\nimport {\n    getAuth,\n    signInWithEmailAndPassword,\n    signOut\n} from &#039;firebase\/auth&#039;;\n\nconst firebaseConfig = {\n    \/\/ apiKey\u7b49\u3092\u8a2d\u5b9a\u3059\u308bFirebase\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u56fa\u6709\u306e\u60c5\u5831\u3002\n};\n\nconst app = initializeApp(firebaseConfig);\nconst db = getFirestore(app);\nconst auth = getAuth(app);\n\n\/\/ \u30b5\u30a4\u30f3\u30a4\u30f3\u51e6\u7406\u3002\nawait signInWithEmailAndPassword(auth, &#039;aa@aa.com&#039;, &#039;password&#039;)\n    .then(async (userCredential) =&gt; {\n        console.log(&#039;userCredential.user.uid = &#039;, userCredential.user.uid);\n        console.log(&#039;\u30ed\u30b0\u30a4\u30f3\u6210\u529f&#039;);\n    })\n    .catch((error) =&gt; {\n        console.log(&#039;\u30ed\u30b0\u30a4\u30f3\u5931\u6557&#039;);\n    });\n\n\/\/ Firestore\u306b\u30c7\u30fc\u30bf\u8ffd\u52a0\u3002\nconst fb_document = doc(db, &#039;collection_name&#039;, &#039;document_name&#039;);\nconst data = {\n    first: &#039;\u30d5\u30a1\u30fc\u30b9\u30c8&#039;,\n    last: &#039;\u30e9\u30b9\u30c8&#039;,\n    born: 2023\n};\nawait setDoc(fb_document, data);\n\n\/\/ Firestore\u306e\u30c7\u30fc\u30bf\u66f4\u65b0\u3002\nconst fb_doc = doc(db, &#039;collection_name&#039;, &#039;document_name&#039;);\nconst updateData = {\n    born: 3024\n};\nawait updateDoc(fb_doc, updateData);\n\n\/\/ Firestore\u304b\u3089collection_name\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u5168\u3066\u8aad\u307f\u8fbc\u307f\u3002\nconst fb_collection = collection(db, &#039;collection_name&#039;);\nconst querySnapShot = await getDocs(fb_collection);\nquerySnapShot.forEach((doc) =&gt; {\n    console.log(&#039;\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u8b58\u5225\u5b50 = &#039;, doc.id);\n    console.log(&#039;born = &#039;, doc.data().born);\n});\n\n\/\/ \u6307\u5b9a\u3057\u305f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u524a\u9664\u3002\nconst fb_del_doc = doc(db, &#039;collection_name&#039;, &#039;document_name&#039;);\nawait deleteDoc(fb_del_doc);\n\n\/\/ \u30b5\u30a4\u30f3\u30a2\u30a6\u30c8\u51e6\u7406\u3002\nsignOut(auth);\n\nReactDOM.createRoot(document.getElementById(&#039;root&#039;)!).render(\n    &lt;React.StrictMode&gt;\n    &lt;\/React.StrictMode&gt;\n);\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>\u76ee\u7684 Firestore\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u969b\u306b\u8a8d\u8a3c\u3092\u5fc5\u8981\u3068\u3057\u305f\u3044\u3002 \u8a8d\u8a3c\u306e\u305f\u3081\u306e\u30ed\u30b0\u30a4\u30f3\u30d7\u30ed\u30d0\u30a4\u30c0\u306f\u30e1\u30fc\u30eb\/\u30d1\u30b9\u30ef\u30fc\u30c9\uff08\u30d1\u30b9\u30ef\u30fc\u30c9\u8a8d\u8a3c\uff09\u306e\u307f\u3002 \u524d\u63d0 Firestore\u306f\u30c6\u30b9\u30c8\u30e2\u30fc\u30c9\u3002 Firestore\u306bcollectio [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[104],"tags":[110,111,105],"class_list":["post-1135","post","type-post","status-publish","format-standard","hentry","category-react","tag-firebase","tag-firestore","tag-react"],"_links":{"self":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/1135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1135"}],"version-history":[{"count":2,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/1135\/revisions"}],"predecessor-version":[{"id":1137,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/1135\/revisions\/1137"}],"wp:attachment":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}