You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
135 lines
4.3 KiB
135 lines
4.3 KiB
import 'dart:convert';
|
|
import 'dart:io';
|
|
import 'dart:math';
|
|
|
|
import 'package:firebase_auth/firebase_auth.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_facebook_auth/flutter_facebook_auth.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:crypto/crypto.dart';
|
|
|
|
class Facebook {
|
|
String? token = '';
|
|
|
|
String name = '';
|
|
String email = '';
|
|
String pictureurl = '';
|
|
String id = '';
|
|
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<bool> signInWithFacebook() async {
|
|
try {
|
|
UserCredential userCredential = await loginFacebook();
|
|
//UserCredential userCredential = await loginGoogle();
|
|
token = await userCredential.user?.getIdToken();
|
|
print('facebook响应数据userCredential: $token');
|
|
if (token != "") {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
} catch (e) {
|
|
print(e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// FaceBook登录
|
|
// 修改后的 signInWithFacebook
|
|
Future<UserCredential> loginFacebook() async {
|
|
//try {
|
|
final rawNonce = generateNonce();
|
|
final nonce = sha256ofString(rawNonce);
|
|
final LoginResult loginResult = await FacebookAuth.instance
|
|
.login(
|
|
loginTracking: LoginTracking.enabled,
|
|
nonce: nonce,
|
|
)
|
|
.catchError((onError) {
|
|
if (kDebugMode) {
|
|
//print(onError);
|
|
}
|
|
throw Exception(onError.message);
|
|
});
|
|
if (loginResult.accessToken == null) {
|
|
throw Exception(loginResult.message);
|
|
}
|
|
// Create a credential from the access token
|
|
OAuthCredential facebookAuthCredential;
|
|
|
|
print("tokenType${loginResult.accessToken!.type}");
|
|
|
|
if (Platform.isIOS) {
|
|
// Create a credential from the access token
|
|
switch (loginResult.accessToken!.type) {
|
|
case AccessTokenType.classic:
|
|
final classicToken = loginResult.accessToken as ClassicToken;
|
|
// token = classicToken.tokenString;
|
|
print('Token 字符串: ${classicToken.tokenString}');
|
|
print('classicToken值: ${classicToken}');
|
|
facebookAuthCredential = FacebookAuthProvider.credential(
|
|
classicToken.authenticationToken!,
|
|
);
|
|
break;
|
|
case AccessTokenType.limited:
|
|
final limitedToken = loginResult.accessToken as LimitedToken;
|
|
//token = limitedToken.tokenString;
|
|
print('Token 字符串: ${limitedToken.tokenString}');
|
|
print('LimitedToken值: ${limitedToken}');
|
|
facebookAuthCredential = OAuthCredential(
|
|
providerId: 'facebook.com',
|
|
signInMethod: 'oauth',
|
|
idToken: limitedToken.tokenString,
|
|
rawNonce: rawNonce,
|
|
);
|
|
break;
|
|
}
|
|
} else {
|
|
facebookAuthCredential = FacebookAuthProvider.credential(
|
|
loginResult.accessToken!.tokenString,
|
|
);
|
|
//token = loginResult.accessToken!.tokenString;
|
|
}
|
|
print('facebookAuthCredential值: ${facebookAuthCredential}');
|
|
if (facebookAuthCredential == null) {
|
|
throw Exception("Facebook Login Fail.");
|
|
}
|
|
final userData = await FacebookAuth.instance.getUserData();
|
|
name = userData["name"].toString();
|
|
|
|
email = userData["email"].toString();
|
|
pictureurl = userData["picture"]["data"]["url"].toString();
|
|
id = userData["id"].toString();
|
|
print("user login facebook ${email}${pictureurl}${name}${id}");
|
|
return FirebaseAuth.instance.signInWithCredential(facebookAuthCredential);
|
|
|
|
// final User? user = userCredential.user; // 获取用户信息
|
|
// token = await userCredential.user?.getIdToken();
|
|
// print("irebaseAuth.instance.signInWithCredential ${token}");
|
|
|
|
// return true;
|
|
// } catch (e) {
|
|
// print("Facebook 登录异常:$e");
|
|
// return false;
|
|
// }
|
|
}
|
|
|
|
// 退出Google登录
|
|
Future<void> signOutFacebook() async {
|
|
await FacebookAuth.instance.logOut();
|
|
}
|
|
}
|
|
|