社交身份驗證是一個多步驟身份驗證流程,允許您將用戶登錄到帳戶或將其與現有帳戶相關聯。
本機平台和 Web 都支持創建憑據,然後可以將其傳遞給signInWithCredential
或linkWithCredential
方法。或者,在 Web 平台上,您可以通過彈出窗口或重定向觸發身份驗證過程。
谷歌
大多數配置已經在使用 Google Sign-In 和 Firebase 時進行了設置,但是您需要確保您的機器的 SHA1 密鑰已配置為與 Android 一起使用。您可以在安裝文檔中查看如何生成密鑰。
確保在Firebase 控制台上啟用了“Google”登錄提供程序。
如果您的用戶在手動註冊帳戶後使用 Google 登錄,由於 Firebase Authentications 受信任提供者的概念,他們的身份驗證提供者將自動更改為 Google。您可以在此處了解更多信息。
iOS+ 和安卓
在本機平台上,需要 3rd 方庫來觸發身份驗證流程。
安裝官方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);
}
在開始之前設置您的Facebook 開發者應用程序並按照設置過程啟用 Facebook 登錄。
確保在Firebase 控制台上啟用了“Facebook”登錄提供程序。使用 Facebook App ID 和 Secret 集。
iOS+ 和安卓
在本機平台上,安裝 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);
}
網絡
在 Web 上,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+ 和安卓
在您開始配置 Sign In with Apple並啟用 Apple 作為登錄提供商之前。
接下來,確保您的Runner
應用程序具有“使用 Apple 登錄”功能。
安裝sign_in_with_apple
插件,以及crypto
包:
dependencies:
sign_in_with_apple: ^3.0.0
crypto: ^3.0.1
import 'dart:convert';
import 'dart:math';
import 'package:crypto/crypto.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:sign_in_with_apple/sign_in_with_apple.dart';
/// Generates a cryptographically secure random nonce, to be included in a
/// credential request.
String generateNonce([int length = 32]) {
const charset =
'0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._';
final random = Random.secure();
return List.generate(length, (_) => charset[random.nextInt(charset.length)])
.join();
}
/// Returns the sha256 hash of [input] in hex notation.
String sha256ofString(String input) {
final bytes = utf8.encode(input);
final digest = sha256.convert(bytes);
return digest.toString();
}
Future<UserCredential> signInWithApple() async {
// To prevent replay attacks with the credential returned from Apple, we
// include a nonce in the credential request. When signing in with
// Firebase, the nonce in the id token returned by Apple, is expected to
// match the sha256 hash of `rawNonce`.
final rawNonce = generateNonce();
final nonce = sha256ofString(rawNonce);
// Request credential for the currently signed in Apple account.
final appleCredential = await SignInWithApple.getAppleIDCredential(
scopes: [
AppleIDAuthorizationScopes.email,
AppleIDAuthorizationScopes.fullName,
],
nonce: nonce,
);
// Create an `OAuthCredential` from the credential returned by Apple.
final oauthCredential = OAuthProvider("apple.com").credential(
idToken: appleCredential.identityToken,
rawNonce: rawNonce,
);
// Sign in the user with Firebase. If the nonce we generated earlier does
// not match the nonce in `appleCredential.identityToken`, sign in will fail.
return await FirebaseAuth.instance.signInWithCredential(oauthCredential);
}
網絡
在您開始配置 Sign In with Apple並啟用 Apple 作為登錄提供商之前。
import 'package:firebase_auth/firebase_auth.dart';
Future<UserCredential> signInWithApple() async {
// Create and configure an OAuthProvider for Sign In with Apple.
final provider = OAuthProvider("apple.com")
..addScope('email')
..addScope('name');
// Sign in the user with Firebase.
return await FirebaseAuth.instance.signInWithPopup(provider);
}
另一種方法是使用signInWithRedirect
。在這種情況下,瀏覽器將離開您的應用程序,您必須在應用程序啟動期間使用getRedirectResult
檢查身份驗證結果。
推特
確保在Firebase 控制台上啟用了“Twitter”登錄提供程序,並設置了 API 密鑰和 API 機密。
iOS+ 和安卓
在本機平台上,安裝 Twitter SDK 和触發身份驗證流程都需要第三方庫。
安裝twitter_login
插件:
dependencies:
twitter_login: ^4.0.1
確保仔細完成twitter_login
的配置步驟,並在Twitter 開發者門戶中使用匹配的 URL 方案註冊回調 URL
import 'package:twitter_login/twitter_login.dart';
Future<UserCredential> signInWithTwitter() async {
// Create a TwitterLogin instance
final twitterLogin = new TwitterLogin(
apiKey: '<your consumer key>',
apiSecretKey:' <your consumer secret>',
redirectURI: '<your_scheme>://'
);
// Trigger the sign-in flow
final authResult = await twitterLogin.login();
// Create a credential from the access token
final twitterAuthCredential = TwitterAuthProvider.credential(
accessToken: authResult.authToken!,
secret: authResult.authTokenSecret!,
);
// Once signed in, return the UserCredential
return await FirebaseAuth.instance.signInWithCredential(twitterAuthCredential);
}
網絡
在網絡上,Twitter SDK 支持使用 Firebase 控制台上提供的 Twitter 應用程序詳細信息自動處理身份驗證流程。確保 Firebase 控制台中的回調 URL 作為回調 URL 添加到您的 Twitter 應用程序的開發者控制台中。
例如:
創建一個 Twitter 提供程序並向signInWithPopup
方法提供憑據。這將觸發一個新窗口出現,提示用戶登錄您的 Twitter 應用程序:
Future<UserCredential> signInWithTwitter() async {
// Create a new provider
TwitterAuthProvider twitterProvider = TwitterAuthProvider();
// Once signed in, return the UserCredential
return await FirebaseAuth.instance.signInWithPopup(twitterProvider);
// Or use signInWithRedirect
// return await FirebaseAuth.instance.signInWithRedirect(twitterProvider);
}
GitHub
確保您已從GitHub 開發人員設置中設置了 OAuth 應用程序,並且在Firebase 控制台上啟用了“GitHub”登錄提供程序,並設置了客戶端 ID 和機密,並在 GitHub 應用程序中設置了回調 URL。
iOS+ 和安卓
對於原生平台,您需要添加google-services.json
和GoogleService-Info.plist
。
對於 iOS,請按照 iOS 指南步驟 1 中的說明添加自定義 URL 方案。
import 'package:github_sign_in/github_sign_in.dart';
Future<UserCredential> signInWithGitHub() async {
// Create a new provider
GithubAuthProvider githubProvider = GithubAuthProvider();
return await _auth.signInWithAuthProvider(githubProvider);
}
網絡
在 Web 上,GitHub SDK 支持使用 Firebase 控制台上提供的 GitHub 應用程序詳細信息自動處理身份驗證流程。確保將 Firebase 控制台中的回調 URL 作為回調 URL 添加到開發者控制台上的 GitHub 應用程序中。
例如:
創建 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);
}