您可以將 Firebase Authentication 與自訂驗證系統整合,方法是修改驗證伺服器,在使用者成功登入時產生自訂簽署權杖。應用程式收到這個權杖後,會使用該權杖向 Firebase 進行驗證。
事前準備
- 如果您尚未將 Firebase 新增至 Android 專案,請先新增。
-
在模組 (應用程式層級) Gradle 檔案 (通常是
<project>/<app-module>/build.gradle.kts或<project>/<app-module>/build.gradle) 中,新增 Android Firebase Authentication 程式庫的依附元件。建議使用 Firebase Android BoM 控制程式庫版本管理。dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:35.1.0")) // Add the dependency for the Firebase Authentication library // When using the BoM, you don't specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-auth") }
只要使用 Firebase Android BoM,應用程式就會一律使用相容的 Firebase Android 程式庫版本。
(替代方案) 新增 Firebase 程式庫依附元件,不使用 BoM
如果選擇不使用 Firebase BoM,則必須在依附元件行中指定每個 Firebase 程式庫版本。
請注意,如果您的應用程式使用多個 Firebase 程式庫,強烈建議您使用 BoM 管理程式庫版本,確保所有版本都相容。
dependencies { // Add the dependency for the Firebase Authentication library // When NOT using the BoM, you must specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-auth:25.0.0") }
- 取得專案的伺服器金鑰:
-
在 Firebase 控制台中,依序前往「
Settings」(設定) >「 Service accounts」(服務帳戶) 分頁。 - 在「Firebase Admin SDK」部分底部,按一下「Generate New Private Key」(產生新的私密金鑰)。
- 新服務帳戶的公開/私密金鑰組會自動儲存至您的電腦。將這個檔案複製到驗證伺服器。
-
在 Firebase 控制台中,依序前往「
使用 Firebase 驗證
- 在登入活動的
onCreate方法中,取得FirebaseAuth物件的共用例項:Kotlin
private lateinit var auth: FirebaseAuth // ... // Initialize Firebase Auth auth = Firebase.auth
Java
private FirebaseAuth mAuth; // ... // Initialize Firebase Auth mAuth = FirebaseAuth.getInstance();
- 初始化 Activity 時,請檢查使用者目前是否已登入:
Kotlin
public override fun onStart() { super.onStart() // Check if user is signed in (non-null) and update UI accordingly. val currentUser = auth.currentUser updateUI(currentUser) }
Java
@Override public void onStart() { super.onStart(); // Check if user is signed in (non-null) and update UI accordingly. FirebaseUser currentUser = mAuth.getCurrentUser(); updateUI(currentUser); }
- 使用者登入應用程式時,請將他們的登入憑證 (例如使用者名稱和密碼) 傳送至驗證伺服器。伺服器會檢查憑證,並在憑證有效時傳回自訂權杖。
- 從驗證伺服器收到自訂權杖後,請將其傳遞至
signInWithCustomToken,讓使用者登入:如果登入成功,Kotlin
customToken?.let { auth.signInWithCustomToken(it) .addOnCompleteListener(this) { task -> if (task.isSuccessful) { // Sign in success, update UI with the signed-in user's information Log.d(TAG, "signInWithCustomToken:success") val user = auth.currentUser updateUI(user) } else { // If sign in fails, display a message to the user. Log.w(TAG, "signInWithCustomToken:failure", task.exception) Toast.makeText( baseContext, "Authentication failed.", Toast.LENGTH_SHORT, ).show() updateUI(null) } } }
Java
mAuth.signInWithCustomToken(mCustomToken) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { // Sign in success, update UI with the signed-in user's information Log.d(TAG, "signInWithCustomToken:success"); FirebaseUser user = mAuth.getCurrentUser(); updateUI(user); } else { // If sign in fails, display a message to the user. Log.w(TAG, "signInWithCustomToken:failure", task.getException()); Toast.makeText(CustomAuthActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); updateUI(null); } } });
AuthStateListener您可以使用getCurrentUser方法取得使用者的帳戶資料。
後續步驟
使用者首次登入後,系統會建立新的使用者帳戶,並連結至使用者登入時使用的憑證,也就是使用者名稱和密碼、電話號碼或驗證供應商資訊。這個新帳戶會儲存為 Firebase 專案的一部分,無論使用者如何登入,都可用來識別專案中所有應用程式的使用者。
-
在應用程式中,您可以從
FirebaseUser物件取得使用者的基本個人資料。請參閱「 管理使用者」。 在 Firebase Realtime Database 和 Cloud Storage 安全性規則中,您可以從
auth變數取得已登入使用者的專屬使用者 ID,並用來控管使用者可存取的資料。
您可以將驗證供應商憑證連結至現有使用者帳戶,允許使用者透過多個驗證供應商登入應用程式。
如要登出使用者,請呼叫
signOut:
Kotlin
Firebase.auth.signOut()
Java
FirebaseAuth.getInstance().signOut();