<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Edwin Liu on Medium]]></title>
        <description><![CDATA[Stories by Edwin Liu on Medium]]></description>
        <link>https://medium.com/@shaizo?source=rss-81487b6cb9b2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*PxWrmeBozwJeZCfDNkuGtQ.jpeg</url>
            <title>Stories by Edwin Liu on Medium</title>
            <link>https://medium.com/@shaizo?source=rss-81487b6cb9b2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 20:23:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shaizo/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[AWS CodeCommit Part 2: using Git with IAM Role on EC2 instances]]></title>
            <link>https://medium.com/@shaizo/aws-codecommit-a2-using-git-with-iam-role-on-ec2-4428024297f4?source=rss-81487b6cb9b2------2</link>
            <guid isPermaLink="false">https://medium.com/p/4428024297f4</guid>
            <category><![CDATA[ami]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[ec2]]></category>
            <category><![CDATA[codecommit]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Edwin Liu]]></dc:creator>
            <pubDate>Thu, 11 Oct 2018 07:42:07 GMT</pubDate>
            <atom:updated>2019-02-13T14:17:22.538Z</atom:updated>
            <content:encoded><![CDATA[<h3>AWS CodeCommit Part 2: using Git with IAM Role on EC2 Instances</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6ZKoy4rofzrtiiYu" /><figcaption>“landscape photography of lake and mountains” by <a href="https://unsplash.com/@joe_leahy?utm_source=medium&amp;utm_medium=referral">Joe Leahy</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>After moving my repo from the private Github account to CodeCommit, I needed a way to pull codes to EC2 instances at the starting up. I have seen people embedded credentials in the code <strong>(not a good way)</strong>, usedotenvor aws configure. The latter two would obviously be better choices for the security but I want more transparency and need this step managed by AWS with the user data. Using role credentials on instance-level provides the benefits of not needing to manage the credentials individually and with the short-term credentials the security increases, which is exactly what I’ve wanted. Setting up this way is straightforward:</p><h3><strong>Step 1. Create an IAM Role</strong></h3><p>First, select the trusted entity for the role. In this case, EC2 instances.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/987/1*fWqZbE5Vw7f4WOcgOkVBfA.png" /><figcaption>Select EC2 instances as the trusted entity</figcaption></figure><p>Then pick or create the permission policy. We are going to read from the repo, so AWSCodeCommitReadOnly will be perfect.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1001/1*vUIrKYbgyb_O3-wviBXayw.png" /><figcaption>Using AWSCodeCommitReadOnly as the policy</figcaption></figure><p>Give it a Role name and that’s it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/977/1*Q0IFkvcHcIMTJB4ERA2_7A.png" /><figcaption>Reviewing the IAM Role</figcaption></figure><h3><strong>Step 2. Attach the Role to the EC2 instance</strong></h3><p>Just select the desired instance and attach the IAM Role created in Step 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/676/1*fO-iQMpPSDovKu-I2vuAiQ.png" /><figcaption>Attach/Replace IAM Role</figcaption></figure><p>The role should be listed automatically. It can also be attached to Launch Configurations as well to be used in Auto Scaling Groups.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/916/1*da9lvwo7L43Hft_LKnDLJA.png" /><figcaption>Selecting the Role</figcaption></figure><h3><strong>Step 3. Configure Git Credential Helper to Use AWS Credential Profiles</strong></h3><p>First, make sure the AWS CLI is installed on the instance. Then we configure the Git credential helper to use AWS credential profile. Since we created the role from IAM, the instance profile has been created already. The helper can be set up thus:</p><pre>git config --global credential.helper &#39;!aws codecommit credential-helper $@&#39; <br>git config --global credential.UseHttpPath true</pre><p>Without a default profile present, the EC2 instance role will be used. To use a different profile for another task, the aws --profile &lt;&lt;profileName&gt;&gt; can be used.</p><p>Now set the git remote URL, and like that, the instance is now using role permission to access the repo:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MovxJM4z3uQAFDORNbNZug.png" /><figcaption>IAM Role permission granting read to git</figcaption></figure><h3>Further Reading</h3><p><a href="https://medium.com/@shaizo/aws-codecommit-moving-from-a-private-github-repo-19a346fa16de">AWS CodeCommit Part 1: Moving from a private GitHub Repo</a></p><h3><strong>References</strong></h3><p><a href="https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2.html#roles-usingrole-ec2instance-roles">https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4428024297f4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[AWS CodeCommit Part 1: Moving from a private GitHub Repo]]></title>
            <link>https://medium.com/@shaizo/aws-codecommit-moving-from-a-private-github-repo-19a346fa16de?source=rss-81487b6cb9b2------2</link>
            <guid isPermaLink="false">https://medium.com/p/19a346fa16de</guid>
            <category><![CDATA[aws-codecommit]]></category>
            <category><![CDATA[github]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[version-control]]></category>
            <category><![CDATA[git]]></category>
            <dc:creator><![CDATA[Edwin Liu]]></dc:creator>
            <pubDate>Mon, 10 Sep 2018 02:36:42 GMT</pubDate>
            <atom:updated>2019-01-03T11:36:50.558Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*VsmymlKgYX8e5pId" /><figcaption>“body of water top view photography” by <a href="https://unsplash.com/@adrienolichon?utm_source=medium&amp;utm_medium=referral">Adrien Olichon</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Our source code was originally managed on GitHub Private. Since a lot of our projects are already running on AWS, we have discussed on the benefit of moving the repos to CodeCommit. The main benefits would be unified (and lesser) costs and the centralized credential management on IAM. Also, the possibility of streamlining the pipeline with CodeDeploy. The steps are as follows:</p><h3>Step 1. Credentials and Login</h3><p>The pre-requisite is to have the accounts/roles associated with the correct permmisions policies. There are currently three types of permissions for CodeCommit: Full access, full access without repo deletion, read-only. Not going to details on how to set up the policies.</p><p>Moving on to the authentication methods, AWS provides a few of them. The most direct way is to generate Git credentials, this option is under Security credentials in IAM:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IT-VpjbU91Lh8d8qzOYOkw.png" /></figure><p>This will generate account/password for HTTPS logins. Skip to Step 2 if that’s what you use.</p><p>My personal preference is SSH, so I useUpload SSH public key, it will show up in the SSH key ID list:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l7uto0B8ZdX1E6JOPnplZA.png" /></figure><p>Then create/edit ~/.ssh/config:</p><pre>Host git-codecommit.*.amazonaws.com<br>User &lt;The SSH key ID shown in the list&gt;<br>IdentityFile &lt;location of the private key, for example: ~/.ssh/codecommit_rsa&gt;</pre><p>We can confirm everything’s working from the terminal:</p><pre>ssh git-codecommit.us-east-2.amazonaws.com</pre><p>Correct result would be similar to the following:</p><pre>You have successfully authenticated over SSH. You can use Git to interact with AWS CodeCommit. <br>Interactive shells are not supported. Connection to git-codecommit.us-east-2.amazonaws.com closed by remote host.</pre><p>CodeCommit can also be accessed with AWS CLI and access key.</p><h3>Step 2. Create a New Repo</h3><p>Very straightforward process. Choose the region, select “Get Started,” give the desired repository name, and done:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/663/1*32hA506blLjFXo3RKovjHw.png" /></figure><p>SNS topic can be optionally created for repo-related events:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/923/1*o3AU4QwBSw8IXYv1MJcJVw.png" /></figure><h3>Step 3. Local Configuration</h3><p>CodeCommit provides information similar to GitHub and GitLab:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*odoSdvoGZciTr4bl5UH_tQ.png" /></figure><p>All that’s left is to update the URL:</p><pre>git remote set-url origin ssh://git-codecommit.ap-northeast-1.amazonaws.com/xxxxxxxxxxxxxxx</pre><h3>Note</h3><p>The documentation mentioned possible issues which I didn’t encounter. Most of them can be fixed by chmod ~.ssh/config or adding SSH Key ID in the URL.</p><h3>Further Reading</h3><p><a href="https://medium.com/@shaizo/aws-codecommit-a2-using-git-with-iam-role-on-ec2-4428024297f4">AWS CodeCommit Part 2: using Git with IAM Role on EC2 Instances</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=19a346fa16de" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Firebase Authentication Part 3: 如何使用 Google Login 與 GitHub Login 認證登入]]></title>
            <link>https://medium.com/@shaizo/firebase-authentication-part-3-using-google-login-and-github-login-927e68f413b7?source=rss-81487b6cb9b2------2</link>
            <guid isPermaLink="false">https://medium.com/p/927e68f413b7</guid>
            <category><![CDATA[google]]></category>
            <category><![CDATA[login]]></category>
            <category><![CDATA[github]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[authentication]]></category>
            <dc:creator><![CDATA[Edwin Liu]]></dc:creator>
            <pubDate>Thu, 22 Mar 2018 02:55:02 GMT</pubDate>
            <atom:updated>2018-03-22T02:57:29.834Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*fzrPN0HgMbqmFmhn." /><figcaption>“A Google search page for the word “analytics” on a mobile phone” by <a href="https://unsplash.com/@edhoradic?utm_source=medium&amp;utm_medium=referral">Edho Pratama</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>把 Google Login 放在 Part 3, 主要是此登入方式在導入 Firebase 時非常直接。Firebase 屬於 Google 服務之一，開啟後內部就可完成設定，所以不需要太多篇幅說明。因此我也把開放社群常用的 GitHub 第三方登入放式一併加入。登入設定完成後的API，請參考 <a href="https://medium.com/@shaizo/firebase-authentication-part-2-using-facebook-login-b2aa7d4bcf38">Firebase Authentication Part 2: 如何使用 Facebook Login 認證登入</a> 一文。我們先從這兩種登入方式的優缺點開始：</p><p><strong>Google Login:<br>優勢：</strong></p><ul><li>大家都有 gmail</li><li>不用承擔帳密保管風險</li><li>匿名性高於 Facebook Login</li><li>帳戶停用機率較低</li></ul><p><strong>風險：</strong></p><ul><li>登入權仍屬於第三方，使用者資料取得需另外處理</li><li>user 資料仍需透過 Google</li></ul><p><strong>GitHub:</strong></p><p><strong>優勢：</strong></p><ul><li>附帶方便的 2-step MFA，提高安全性很多</li><li>如果是技術性的應用程式，可提升專業度</li></ul><p><strong>風險：</strong></p><ul><li>一般 user 不太會有帳號</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7aeXRcCruScQaPKAEL1rTg.png" /></figure><p>Google登入方式就是這樣：勾選啟用、儲存。</p><p>如果有使用其他專案或 Web SDK ，可以在下方兩個選項中加入就完成了。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dk3bWLDSVg6KlJP-uIsVNg.png" /></figure><p>GitHub 登入反而需要較多設定步驟。</p><p>首先在 Firebase 介面中啟用 GitHub Login, 並拷貝最下方的網址。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zHqls1brcRbsBuz08OulOw.png" /></figure><p>接著到 <a href="https://github.com/settings/developers">GitHub 開發者專頁</a>，選擇 New OAuth App 新增一個OAuth應用程式。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZCVRJVh40QcaNYXH70EieA.png" /></figure><p>把 Firebase 提供的連結貼入 Authorization callback URL，填寫其他欄位並 Register application。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/788/1*UMa_e2hG-3ShWm3rOn-dcA.png" /></figure><p>最後再把產生的兩個欄位貼回 Firebase就完成了。下方兩個按鈕還可以取消 token 或重設 client secret。如果 user 有設定 2-step MFA，在登入時也會跳出 Authenticator 認證，增加許多安全性。</p><p>下一篇開始會討論程式與架構方面的議題。</p><p><strong>衍伸閱讀：</strong></p><p><a href="https://medium.com/@shaizo/firebase-authentication-part-1-using-email-password-a9837a788ea5">Firebase Authentication Part 1: 如何使用電子郵件/密碼認證登入</a></p><p><a href="https://medium.com/@shaizo/firebase-authentication-part-2-using-facebook-login-b2aa7d4bcf38">Firebase Authentication Part 2: 如何使用 Facebook Login 認證登入</a></p><p><a href="https://medium.com/@shaizo/firebase-authentication-part-3-using-google-login-and-github-login-927e68f413b7">Firebase Authentication Part 3: 如何使用 Google Login 與 GitHub Login 認證登入</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=927e68f413b7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Firebase Authentication Part 2: 如何使用 Facebook Login 認證登入]]></title>
            <link>https://medium.com/@shaizo/firebase-authentication-part-2-using-facebook-login-b2aa7d4bcf38?source=rss-81487b6cb9b2------2</link>
            <guid isPermaLink="false">https://medium.com/p/b2aa7d4bcf38</guid>
            <category><![CDATA[oauth]]></category>
            <category><![CDATA[facebook]]></category>
            <category><![CDATA[authentication]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[login]]></category>
            <dc:creator><![CDATA[Edwin Liu]]></dc:creator>
            <pubDate>Wed, 14 Mar 2018 14:19:40 GMT</pubDate>
            <atom:updated>2018-03-22T02:58:00.884Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*SoMTKwHp0QzFx5zh." /><figcaption>Photo by <a href="https://unsplash.com/@timbennettcreative?utm_source=medium&amp;utm_medium=referral">Tim Bennett</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>因為第三方登入對 user 非常友善，使用者不需另設密碼，應用端也不用承擔保管帳密的風險，我個人是非常推薦的。加上人人幾乎都有 Facebook 帳號，Facebook 可說其中最主流的第三方登入。Facebook 本身又帶有 tracking，在行銷上有很多附加優點，所以很值得做為預設登入方式之一。本文專注在以 Firebase 驗證 Facebook Login，Facebook Developer 另有許多進階設定，就不在本文探討範圍內。最後，在文末我簡單描述了 Firebase 認證多重帳號時需要特殊處理的情況，解決方式我再另文詳述。</p><p><strong>Update: 補上Facebook Login的優勢與風險</strong>：<strong><br>優勢：</strong></p><ul><li>普及率高</li><li>不用承擔帳密保管風險</li><li>附有 Facebook 內建使用者行為分析</li></ul><p><strong>風險：</strong></p><ul><li>不具匿名性<br>Facebook 採實名制，所以使用者條款可能須針對個資使用說明</li><li>Facebook 帳號被停用時，需要有對應的 flow 避免使用者無法登入<br>雖然其他種認證也會有此情形，可以一併思考另外的流程</li></ul><p>首先很快地解釋兩個用語：</p><p><strong>OAuth:</strong> 常用的開放認證標準。使用 Firebase 或其他驗證服務的好處，就是不需要自己重刻 OAuth 的協定，降低複雜度。</p><p><strong>Federated Identity Provider:</strong> 提供驗證的第三方服務。如 Google, Facebook。</p><p>1. 自 <a href="https://developers.facebook.com/">Facebook Developers</a> 新增應用程式。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_p9RSjIXXNDS1p5Dfulp2A.png" /></figure><p>2. 取得應用程式的 <strong>應用程式編號 Facebook APP ID</strong> 與 <strong>應用程式密鑰 APP secret</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NtZojdaG-2OLUn_AaEBUbQ.png" /></figure><p>3. 回到 Firebase Authentication 設定頁</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VRu0vy75NttHZS2Zn2BecQ.png" /></figure><p>啟用 Facebook，將上步驟兩筆資料貼入對應的位置。</p><p>紀錄此頁面的 OAuth 重新導向 (Redirect) URI。</p><p>5. 把 URI 貼回 Facebook Developer 中的 Facebook Login 設定</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5KyLGfcEXnldve-lI1g9Kw.png" /></figure><p>雖然 OAuth 協定中就包含 Redirect URI，此設定的好處可以限制在此應用程式中實際可用的 URI，增加安全性。</p><p>6. 到此設定完成，可以開始串接。</p><p>使用者的身份會由 Facebook 確認，所以不需區分新增、登入。</p><p>在界面上，Firebase 提供兩種不同的呈現方式：</p><p><strong>signInWithPopup:</strong> 以 popup 形式跳出登入畫面。</p><p><strong>signInWithRedirect:</strong> 重新導向到登入畫面後再倒回。</p><p>一般瀏覽器上就是出現 popup 或導入其他頁面的差別，在行動裝置上的 UX 則影響較大，在設計上可花點功夫思考。</p><p>下圖為 signInWithPopup：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3gaf42IEUq8eiOdpG8x7EA.png" /></figure><p>7. 這就是透過 Firebase 認證 Facebook Login 的方法。</p><p>當然如果你透過前一篇文章已設定好電子郵件與密碼，現在又想透過 Facebook Login 測試用同一個 email 臉書帳號的話，就會出現無法登入的問題，這也是本文一開始提到的特性。</p><p>Firebase 預設單一email就是一個帳戶，所以登入時會出現帳號已透過其他認證方式存在的 error：</p><pre>auth/account-exists-with-different-credential</pre><p>就目前階段這是正常結果。</p><p>雖然可在 Firebase 設定中改成一個 email 可建立多個帳戶，但這會造成不同登入產生不同uid，使同一使用者用不同登入被判斷成不同帳號。</p><p>正規解決方式需要花點功夫，改日再更詳細說明。</p><p><strong>衍伸閱讀：</strong></p><p><a href="https://medium.com/@shaizo/firebase-authentication-part-1-using-email-password-a9837a788ea5">Firebase Authentication Part 1: 如何使用電子郵件/密碼認證登入</a></p><p><a href="https://medium.com/@shaizo/firebase-authentication-part-2-using-facebook-login-b2aa7d4bcf38">Firebase Authentication Part 2: 如何使用 Facebook Login 認證登入</a></p><p><a href="https://medium.com/@shaizo/firebase-authentication-part-3-using-google-login-and-github-login-927e68f413b7">Firebase Authentication Part 3: 如何使用 Google Login 與 GitHub Login 認證登入</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b2aa7d4bcf38" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Firebase Authentication Part 1: 如何使用電子郵件/密碼認證登入]]></title>
            <link>https://medium.com/@shaizo/firebase-authentication-part-1-using-email-password-a9837a788ea5?source=rss-81487b6cb9b2------2</link>
            <guid isPermaLink="false">https://medium.com/p/a9837a788ea5</guid>
            <category><![CDATA[authentication]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[firebase]]></category>
            <dc:creator><![CDATA[Edwin Liu]]></dc:creator>
            <pubDate>Mon, 05 Mar 2018 05:22:04 GMT</pubDate>
            <atom:updated>2018-03-22T02:58:16.278Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Drwm4SwBF1FLsbff." /><figcaption>Photo by <a href="https://unsplash.com/@sanwaldeen?utm_source=medium&amp;utm_medium=referral">Sanwal Deen</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Firebase 提供服務包含 Analytics, Auth, Database, Storage, Notifications，很有吸引力，不需要自己重複架設後端架構。最近聽到身邊有朋友因為沒接觸過，擔心串接會很花時間、需要學習很多。但其實 Firebase 的優點在於導入很直覺。雖然特定功能目前只支援 mobile，但 Auth, Database 等常用功能都可直接對應 web，非常方便。這系列想先從 authentication 裏最常見的電子郵件/密碼 email/password 登入開始，再衍伸到如何使用常見第三方開放認證及解決同電子郵件多重登入方式。提供有興趣的朋友參考。後續有機會再延伸討論到 Database。範例都以我最熟悉的 ES6 以上 JavaScript 為主。</p><ol><li>新增專案</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1010/1*gPE8nwUARM2wZDUfjdMoqw.png" /></figure><p>此步驟無特別需注意的地方。Firebase 會依照專案名稱自動生成 ID。文件說明中寫到「地區」影響的是幣值等單位，跟實際儲存位置無關。</p><p>2. 選擇「電子郵件/密碼」認證方式</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ySQoOkE8aHRk1ENALlK3xQ.png" /></figure><p>側邊欄 Authentication 點入，登入方式中可以看到目前支援的認證方式。就「電子郵件/密碼」來說，直接開啟就可使用，不需另外設定。驗證、救援等功能，可透過 SDK 設定，這裡就不說明了。</p><p>「已授權網域」在 local 以外的環境就需要設定，否則 OAuth 時會出現問題。如果 production 連不上可以檢查一下這裡。</p><p>「進階」設定裏「每個電子郵件一個帳戶」可以控制使用者註冊時的行為，比較常見的做法會是「允許使用者用同一個電子郵件地址建立多個帳戶」。這算比較大的議題，後續再補充討論。</p><p>3. 後台手動新增帳號</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SrBktFrr4WhA-gQC8LrrLg.png" /></figure><p>「登入方式」設定好後，回「使用者」就可以手動新增使用者。</p><p>此頁面也可以依照不同登入方式做基本的管理。</p><p>4. API 串接設定</p><p>Authentication 右側「網路設定」可以取得連線資訊：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9faa3f9e1f9d4ea06b8e3a26cd8640d1/href">https://medium.com/media/9faa3f9e1f9d4ea06b8e3a26cd8640d1/href</a></iframe><p>我會將資訊另外複製到自己專案中，用 dotenv 管理並用 server-side authentication。如果直接使用，請確認並了解 client-side authentication 的風險。</p><p>到此已完成設定，註冊與登入非常直接，兩者都會回覆關於 user 的 Promise：</p><p>5. 透過 API 註冊帳號</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc3c63a0b4d768f214a797c0c2fd6d3b/href">https://medium.com/media/cc3c63a0b4d768f214a797c0c2fd6d3b/href</a></iframe><p><strong>可能錯誤：</strong></p><p><strong>auth/email-already-in-use</strong> 帳號已存在<br><strong>auth/invalid-email email </strong>不符規定<br><strong>auth/operation-not-allowed</strong> 未啟用「電子郵件/密碼」登入方式。請參考步驟 2<br><strong>auth/weak-password</strong> 密碼太弱</p><p>6. 透過 API 登入</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9192a3951d84fe053278f1e8550cf217/href">https://medium.com/media/9192a3951d84fe053278f1e8550cf217/href</a></iframe><p><strong>可能錯誤：</strong></p><p><strong>auth/invalid-email email </strong>不符規定<br><strong>auth/user-disabled</strong> 該 email 已被停用<br><strong>auth/user-not-found</strong> 找不到該使用者<br><strong>auth/wrong-password</strong> 密碼錯誤</p><p>7. 透過 API 取得使用者 UID</p><pre>firebase.auth().uid </pre><p>登入後可用此方式取得 uid 作為 key。</p><p><strong>衍伸閱讀：</strong></p><p><a href="https://medium.com/@shaizo/firebase-authentication-part-1-using-email-password-a9837a788ea5">Firebase Authentication Part 1: 如何使用電子郵件/密碼認證登入</a></p><p><a href="https://medium.com/@shaizo/firebase-authentication-part-2-using-facebook-login-b2aa7d4bcf38">Firebase Authentication Part 2: 如何使用 Facebook Login 認證登入</a></p><p><a href="https://medium.com/@shaizo/firebase-authentication-part-3-using-google-login-and-github-login-927e68f413b7">Firebase Authentication Part 3: 如何使用 Google Login 與 GitHub Login 認證登入</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a9837a788ea5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[談 JavaScript Promise 的 Callback Functions]]></title>
            <link>https://medium.com/@shaizo/javascript-promise-callback-functions-9b30fd3b4652?source=rss-81487b6cb9b2------2</link>
            <guid isPermaLink="false">https://medium.com/p/9b30fd3b4652</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[callback]]></category>
            <category><![CDATA[promises]]></category>
            <dc:creator><![CDATA[Edwin Liu]]></dc:creator>
            <pubDate>Thu, 22 Feb 2018 05:36:02 GMT</pubDate>
            <atom:updated>2018-03-05T06:44:39.835Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*4p8CXdBCWI1EF2c5." /><figcaption>Photo by <a href="https://unsplash.com/@clemhlrdt?utm_source=medium&amp;utm_medium=referral">Clément H</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>JavaScript 的 Promise 確實是好用的 feature，簡化 asynchronous callback 的寫法。最近參與的一則討論很有趣，JavaScript API 中有些 arguments 要傳入的是 callback functions。初接觸時可能會誤以為要提供 function call，所以在其他地方先定義了 function，然後塞在 arguments 裡面。其實該提供的是 function declaration/expression/arrow function。而且因為 function call 被評估時就會執行，所以有時很難發現問題。而 Promise 就是以 callback 為基礎，所以也會出現這種狀況。MDN 的 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises">Promise 頁面</a>寫的是：</p><blockquote>Essentially, a promise is a returned object to which you attach callbacks, instead of passing callbacks into a function.</blockquote><p>而這次引起討論的 code，目的是刪除 Firebase 資料後退回前一頁，剛好就會發生此情況：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8378c6b7b694832a4d7c77e6d1b5d5b6/href">https://medium.com/media/8378c6b7b694832a4d7c77e6d1b5d5b6/href</a></iframe><p>其中 then 寫法所產生的疑問是，為何需用 function expression?而不能直接寫成</p><p>.then(Actions.pop())</p><p>不是都得到同樣的結果？結果一樣刪除紀錄並退回前一頁？</p><p>經驗告訴我們，這裡該用expression才對。不過這裡如果誤用了function call，不只不會有error，而且正常流程下user也會看到預期的流程。嗯？</p><p>我用個實驗做例子，結果可能出乎預想：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7d8174e95326354e5ba4541f9abcde51/href">https://medium.com/media/7d8174e95326354e5ba4541f9abcde51/href</a></iframe><p>Log：</p><pre>Starting: Wed Feb 21 2018 16:51:58 GMT+0800 (CST)<br>Resolve A should not be called: Wed Feb 21 2018 16:51:58 GMT+0800 (CST)<br>Reject A: Wed Feb 21 2018 16:52:00 GMT+0800 (CST)<br>Reject B: Wed Feb 21 2018 16:52:00 GMT+0800 (CST)</pre><p>雖然 sleep() 只 reject，但第一種寫法：<br>1. 立刻就執行了 resolve<br>2. Promise returned 以後依然（正確地）把 reject 再執行了一次。</p><p>所以第二種寫法才實際符合Promise的預期結果，於兩秒鐘後執行 reject。</p><p>也就是說 Firebase 的例子中，送出 remove 跟退回前一頁幾乎同時發生，並沒有等 Firebase 的 return。在沒有出現錯誤時，看起來行為確實符合預期，一樣刪除且退回，很難發現問題。但 Firebase 傳回錯誤時，卻會早已退回前一頁，無法在刪除頁通知 user 狀況。這屬於計畫外的情況，值得特別注意。</p><p>PS. 使用 npm@5.2.0 以上，可以直接在 console 中以 npx 執行此實驗（<em>內容為上述 code, 可直接點進 gist 確認一下，是個好習慣喔</em>）：</p><pre>npx <a href="https://gist.github.com/cslasher/2c1244cab67d9b1c099f0128517d8615">https://gist.github.com/cslasher/2c1244cab67d9b1c099f0128517d8615</a></pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9b30fd3b4652" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>