Firebase Google登入
繼之前介紹 Firebase 的 Email 登入和匿名登入後
這篇要示範的是 Google 登入
主要可以分成下列幾個環節:
個人覺得有點小複雜,原因正如我之前說的
Firebase 和 google 之間多了 Expo 的交接
需要一些 firebase 官網沒有提供的環節
像是 user 物件的內容就不一樣
我需要兩邊 doc 來回查才知道到底是怎樣
這真的要記錄下來
不然我大概一下就忘了
一、開啟 google API
參考 link
1. 首先到 google 的 credentials page
2. 建立專案


這邊無法選擇內部,因此放心選外部
後續還有一些步驟但不成問題
3. 回到憑證頁面 -> 建立憑證 -> 選 OAuth 用戶端 ID

4. 依照手機系統選擇 Android / iOS

5. 設定套件名稱&憑證指紋
- 套件名稱填
host.exp.exponent
- 在 terminal 中 run
openssl rand -base64 32 | openssl sha1 -c
並將 output 填入 憑證指紋
二、使用 Expo 的 Google Auth SDK
參考 link
expo install expo-google-app-auth
1 | import * as Google from 'expo-google-app-auth'; |
用戶端編號可以從 Google API 的憑證頁面中找到
三、使用 Firebase 的登入流程
參考 link
firebase 提供的 popup/redirect 兩種方法
只能用在 web 上,在手機上會報錯:
Error: This operation is not supported in the environment this application is running on. "location.protocol" must be http, https or chrome-extension and web storage must be enabled.
所以我們在行動端只能用進階做法
1. 到 firebase console 開啟 Google sign in 的功能
appId 和 appKey 可以在 Google API 頁面找到
2. 定義 onSignIn()
1 | function onSignIn(googleUser) { |
為什麼
unsubscribe
裡面還有unsubscribe
? linkGoogleAuthProvider.credential()
有兩個input,idToken
和accessToken
,官網給的寫法是googleUser.getAuthResponse().id_token
,但因為我們用的是 ExpoGoogle.logInAsync
回傳的googleUser
,他不含任何函式,自然就沒有getAuthResponse()
可以用。 link
3. 定義 isUserEqual()
1 | function isUserEqual(googleUser, firebaseUser) { |
- 這邊的
googleUser.user.id
也是上述的原因
4. 把onSignIn()
放進 signInWithGoogleAsync()
裡並傳入 user 物件
1 | async function signInWithGoogleAsync() { |
完成! 可以到這裡參考我的範例