你好,我有这个功能可以登录facebook,但它返回了一个UserCredential,是否可以从firebase_auth包将其转换为User?
Future<UserCredential> facebook() async {
try {
final AccessToken accessToken = await FacebookAuth.instance.login();
// Create a credential from the access token
final FacebookAuthCredential credential = FacebookAuthProvider.credential(
accessToken.token,
);
// Once signed in, return the UserCredential
UserCredential authResult =
await _firebaseAuth.signInWithCredential(credential);
} on FacebookAuthException catch (e) {
// handle the FacebookAuthException
} on FirebaseAuthException catch (e) {
// handle the FirebaseAuthException
} finally {}
return null;
}
UserCredential
实际上有一个用于User
的getter。
final AccessToken accessToken = await FacebookAuth.instance.login();
final AuthCredential credential =
FacebookAuthProvider.getCredential(accessToken: accessToken.token);
final firebaseUser = (await auth.signInWithCredential(credential)).user;