社交身份驗證是一種多步驟身份驗證流程,可讓您將使用者登入帳戶或將其與現有帳戶連結。
本機平台和 Web 都支援建立憑證,然後可以將其傳遞給signInWithCredential
或linkWithCredential
方法。或者,在網路平台上,您可以透過彈出視窗或重定向來觸發身份驗證過程。
使用 Google Sign-In with Firebase 時,大多數設定均已設置,但您需要確保電腦的 SHA1 金鑰已配置為與 Android 一起使用。您可以在身份驗證文件中了解如何產生金鑰。
確保在Firebase 控制台上啟用「Google」登入提供者。
如果您的使用者使用 Google 登錄,則在手動註冊帳戶後,由於 Firebase 驗證可信任提供者的概念,其驗證提供者將自動變更為 Google。您可以在此處了解更多相關資訊。
iOS+ 和 Android
在本機平台上,需要第三方函式庫來觸發身分驗證流程。
安裝官方google_sign_in
外掛。
安裝後,觸發登入流程並建立新的憑證:
import 'package:google_sign_in/google_sign_in.dart';
Future<UserCredential> signInWithGoogle() async {
// Trigger the authentication flow
final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();
// Obtain the auth details from the request
final GoogleSignInAuthentication? googleAuth = await googleUser?.authentication;
// Create a new credential
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth?.accessToken,
idToken: googleAuth?.idToken,
);
// Once signed in, return the UserCredential
return await FirebaseAuth.instance.signInWithCredential(credential);
}
網路
在網路上,Firebase SDK 支援使用 Firebase 專案自動處理身份驗證流程。例如:
建立 Google 驗證提供程序,提供您希望從使用者獲得的任何其他權限範圍:
GoogleAuthProvider googleProvider = GoogleAuthProvider();
googleProvider.addScope('https://www.googleapis.com/auth/contacts.readonly');
googleProvider.setCustomParameters({
'login_hint': 'user@example.com'
});
向signInWithPopup
方法提供憑證。這將觸發一個新視窗出現,提示使用者登入您的專案。或者,您可以使用signInWithRedirect
將身份驗證過程保留在同一視窗中。
Future<UserCredential> signInWithGoogle() async {
// Create a new provider
GoogleAuthProvider googleProvider = GoogleAuthProvider();
googleProvider.addScope('https://www.googleapis.com/auth/contacts.readonly');
googleProvider.setCustomParameters({
'login_hint': 'user@example.com'
});
// Once signed in, return the UserCredential
return await FirebaseAuth.instance.signInWithPopup(googleProvider);
// Or use signInWithRedirect
// return await FirebaseAuth.instance.signInWithRedirect(googleProvider);
}
谷歌玩遊戲
您可以使用 Play 遊戲登入對 Android 遊戲中的使用者進行身份驗證。
安卓
請按照 Android 上的 Google 設定說明進行操作,然後使用您的 Firebase 應用程式資訊設定 Play 遊戲服務。
以下內容將觸發登入流程,建立新憑證並登入使用者:
final googleUser = await GoogleSignIn(
signInOption: SignInOption.games,
).signIn();
final googleAuth = await googleUser?.authentication;
if (googleAuth != null) {
// Create a new credential
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
// Once signed in, return the UserCredential
await _auth.signInWithCredential(credential);
}
在開始之前,請設定您的Facebook 開發者應用程式並按照設定流程啟用 Facebook 登入。
確保在Firebase 控制台上啟用「Facebook」登入提供者。與 Facebook App ID 和 Secret 設定。
iOS+ 和 Android
在本機平台上,需要第三方程式庫來安裝 Facebook SDK 並觸發身分驗證流程。
您需要按照外掛程式文件中的步驟操作,以確保 Android 和 iOS Facebook SDK 都已正確初始化。完成後,觸發登入流程,建立 Facebook 憑證並讓使用者登入:
import 'package:flutter_facebook_auth/flutter_facebook_auth.dart';
Future<UserCredential> signInWithFacebook() async {
// Trigger the sign-in flow
final LoginResult loginResult = await FacebookAuth.instance.login();
// Create a credential from the access token
final OAuthCredential facebookAuthCredential = FacebookAuthProvider.credential(loginResult.accessToken.token);
// Once signed in, return the UserCredential
return FirebaseAuth.instance.signInWithCredential(facebookAuthCredential);
}
網路
在網路上,Firebase SDK 支援使用 Firebase 控制台上提供的 Facebook 應用程式詳細資訊自動處理身份驗證流程。例如:
建立 Facebook 提供程序,提供您希望從使用者獲得的任何其他權限範圍。
確保來自 Firebase 控制台的 OAuth 重定向 URI 新增為 Facebook 應用程式中的有效 OAuth 重定向 URI。
FacebookAuthProvider facebookProvider = FacebookAuthProvider();
facebookProvider.addScope('email');
facebookProvider.setCustomParameters({
'display': 'popup',
});
向signInWithPopup
方法提供憑證。這將觸發一個新視窗出現,提示用戶登入您的 Facebook 應用程式:
Future<UserCredential> signInWithFacebook() async {
// Create a new provider
FacebookAuthProvider facebookProvider = FacebookAuthProvider();
facebookProvider.addScope('email');
facebookProvider.setCustomParameters({
'display': 'popup',
});
// Once signed in, return the UserCredential
return await FirebaseAuth.instance.signInWithPopup(facebookProvider);
// Or use signInWithRedirect
// return await FirebaseAuth.instance.signInWithRedirect(facebookProvider);
}
蘋果
iOS+
在開始之前,請設定「使用 Apple 登入」並啟用 Apple 作為登入提供者。
接下來,確保您的Runner
應用程式具有「使用 Apple 登入」功能。
安卓
在開始之前,請設定「使用 Apple 登入」並啟用 Apple 作為登入提供者。
網路
在開始之前,請設定「使用 Apple 登入」並啟用 Apple 作為登入提供者。
import 'package:firebase_auth/firebase_auth.dart';
Future<UserCredential> signInWithApple() async {
final appleProvider = AppleAuthProvider();
if (kIsWeb) {
await FirebaseAuth.instance.signInWithPopup(appleProvider);
} else {
await FirebaseAuth.instance.signInWithProvider(appleProvider);
}
}
撤銷 Apple 身份驗證令牌
Apple 平台上的 Apple 登入會傳回授權代碼,可用於使用revokeTokenWithAuthorizationCode()
API 撤銷 Apple 驗證令牌。
import 'package:firebase_auth/firebase_auth.dart';
Future<UserCredential> signInWithApple() async {
final appleProvider = AppleAuthProvider();
UserCredential userCredential = await FirebaseAuth.instance.signInWithPopup(appleProvider);
// Keep the authorization code returned from Apple platforms
String? authCode = userCredential.additionalUserInfo?.authorizationCode;
// Revoke Apple auth token
await FirebaseAuth.instance.revokeTokenWithAuthorizationCode(authCode!);
}
微軟
iOS+
在開始之前,設定 Microsoft Login for iOS並將自訂 URL 方案新增至您的執行程式(步驟 1) 。
安卓
在開始設定 Microsoft Login for Android之前。
不要忘記添加應用程式的 SHA-1 指紋。
網路
import 'package:firebase_auth/firebase_auth.dart';
Future<UserCredential> signInWithMicrosoft() async {
final microsoftProvider = MicrosoftAuthProvider();
if (kIsWeb) {
await FirebaseAuth.instance.signInWithPopup(microsoftProvider);
} else {
await FirebaseAuth.instance.signInWithProvider(microsoftProvider);
}
}
推特
確保在Firebase 控制台上啟用「Twitter」登入提供者並設定 API 金鑰和 API 金鑰。確保您的 Firebase OAuth 重新導向 URI(例如 my-app-12345.firebaseapp.com/__/auth/handler)在Twitter 應用程式的 config上的應用程式設定頁面中設定為授權回呼 URL。
您可能還需要請求提升的API 存取權限,具體取決於您的應用程式。
iOS+
您需要依照 iOS 指南步驟 1 所述設定自訂 URL 方案。
安卓
如果您尚未指定已套用的 SHA-1 指紋,請從 Firebase 控制台的「設定」頁面執行此操作。有關如何取得應用程式的 SHA-1 指紋的詳細信息,請參閱驗證您的客戶端。
網路
開箱即用。
import 'package:firebase_auth/firebase_auth.dart';
Future<void> _signInWithTwitter() async {
TwitterAuthProvider twitterProvider = TwitterAuthProvider();
if (kIsWeb) {
await FirebaseAuth.instance.signInWithPopup(twitterProvider);
} else {
await FirebaseAuth.instance.signInWithProvider(twitterProvider);
}
}
GitHub
確保您已從GitHub 開發人員設定中設定 OAuth 應用程序,並且在Firebase 控制台上啟用了“GitHub”登入提供程序,並設定了客戶端 ID 和金鑰,並在 GitHub 應用程式中設定了回調 URL。
iOS+ 和 Android
對於本機平台,您需要新增google-services.json
和GoogleService-Info.plist
。
對於 iOS,請按照 iOS 指南步驟 1 中的說明新增自訂 URL 方案。
Future<UserCredential> signInWithGitHub() async {
// Create a new provider
GithubAuthProvider githubProvider = GithubAuthProvider();
return await FirebaseAuth.instance.signInWithProvider(githubProvider);
}
網路
在網路上,GitHub SDK 支援使用 Firebase 控制台上提供的 GitHub 應用程式詳細資訊自動處理驗證流程。確保將 Firebase 控制台中的回呼 URL 新增為開發者控制台上 GitHub 應用程式中的回調 URL。
例如:
建立 GitHub 提供者並向signInWithPopup
方法提供憑證。這將觸發一個新視窗出現,提示使用者登入您的 GitHub 應用程式:
Future<UserCredential> signInWithGitHub() async {
// Create a new provider
GithubAuthProvider githubProvider = GithubAuthProvider();
// Once signed in, return the UserCredential
return await FirebaseAuth.instance.signInWithPopup(githubProvider);
// Or use signInWithRedirect
// return await FirebaseAuth.instance.signInWithRedirect(githubProvider);
}
雅虎
確保在Firebase 控制台上啟用「Yahoo」登入提供者並設定 API 金鑰和 API 金鑰。請同時確保您的 Firebase OAuth 重定向 URI(例如 my-app-12345.firebaseapp.com/__/auth/handler)在套用的 Yahoo Developer Network 設定中設定為重定向 URI。
import 'package:firebase_auth/firebase_auth.dart';
Future<UserCredential> signInWithYahoo() async {
final yahooProvider = YahooAuthProvider();
if (kIsWeb) {
await _auth.signInWithPopup(yahooProvider);
} else {
await _auth.signInWithProvider(yahooProvider);
}
}
使用 OAuth 存取令牌
透過使用 AuthProvider,您可以透過發出下列請求來擷取與提供者關聯的存取權杖。
final appleProvider = AppleAuthProvider();
final user = await FirebaseAuth.instance.signInWithProvider(appleProvider);
final accessToken = user.credential?.accessToken;
// You can send requests with the `accessToken`
連結身份驗證提供者
如果要將提供者連結到目前用戶,可以使用以下方法:```dart wait FirebaseAuth.instance.signInAnonymously();
最終 appleProvider = AppleAuthProvider();
if (kIsWeb) { 等待 FirebaseAuth.instance.currentUser?.linkWithPopup(appleProvider);
// 您也可以使用linkWithRedirect
} else { wait FirebaseAuth.instance.currentUser?.linkWithProvider(appleProvider); }
// 您的匿名用戶現已升級為能夠透過 Sign In With Apple 進行連線 ```
向提供者重新進行身份驗證
相同的模式可以與reauthenticateWithProvider
一起使用,它可用於檢索需要最近登入的敏感操作的新憑證。
final appleProvider = AppleAuthProvider();
if (kIsWeb) {
await FirebaseAuth.instance.currentUser?.reauthenticateWithPopup(appleProvider);
// Or you can reauthenticate with a redirection
// await FirebaseAuth.instance.currentUser?.reauthenticateWithRedirect(appleProvider);
} else {
await FirebaseAuth.instance.currentUser?.reauthenticateWithProvider(appleProvider);
}
// You can now perform sensitive operations