在我的移动应用程序中,我有 4 种登录方法:
问题是,例如,当我使用 Google Sign In 和 [email protected] 创建帐户时,然后我注销并尝试使用使用同一电子邮件创建的 Facebook 帐户登录,就会出现错误。总结一下每个登录功能都是独立工作的。
我想知道是否可以通过云功能来处理这个问题?
这是颤振代码:
///GOOGLE
static Future<bool> signWithGoogle() async {
// return false;
final GoogleSignIn _googleSignIn = GoogleSignIn( scopes: ['email'] );
GoogleSignInAccount? account = await _googleSignIn.signIn();
googleSignInAccount = account;
if (account == null) return false;
GoogleSignInAuthentication authentication = await account.authentication;
String? email = _googleSignIn.currentUser!.email;
final bool firstLogin = await isFirstLogin(email);
ParseResponse response;
if (firstLogin){
response = await ParseUser.loginWith(
'google',
google(
authentication.accessToken!,
_googleSignIn.currentUser!.id,
authentication.idToken!
),
username: _googleSignIn.currentUser!.displayName,
email: email,
);
}else {
response = await ParseUser.loginWith(
'google',
google(
authentication.accessToken!,
_googleSignIn.currentUser!.id,
authentication.idToken!
),
);
}
if (response.success){
return _setDeviceId(response.result);
}else
_handleError(response);
return false;
}
static Future<GoogleSignInAccount?> googleForDataOnly() async {
final GoogleSignIn _googleSignIn = GoogleSignIn( scopes: ['email'] );
return await _googleSignIn.signIn();
}
///FACEBOOK
static Future<bool> signWithFacebook() async {
final LoginResult result = await FacebookAuth.instance.login(permissions: [
'email',
'public_profile',
'user_location'
]);
if (result.status == LoginStatus.cancelled) return false;
if (result.status == LoginStatus.success) {
if (result.accessToken == null) {
Fluttertoast.showToast(msg: Translate.unknownError());
return false;
}
final userData = await FacebookAuth.instance.getUserData();
String? email = userData['email'];
if (email == null || email.isEmpty){
Fluttertoast.showToast(msg: Translate.emailPermissionRequired());
return false;
}
final bool firstLogin = await isFirstLogin(email);
ParseResponse response;
if (firstLogin){
response = await ParseUser.loginWith(
'facebook',
facebook(result.accessToken!.token,
result.accessToken!.userId,
result.accessToken!.expires
),
email: email,
username: userData['name']
);
}else {
response = await ParseUser.loginWith(
'facebook',
facebook(result.accessToken!.token,
result.accessToken!.userId,
result.accessToken!.expires
),
);
}
if (response.success) return _setDeviceId(response.result);
else _handleError(response);
}
return false;
}
static Future<bool> facebookDataOnly() async {
final LoginResult result = await FacebookAuth.instance.login(permissions: [
'email',
'public_profile',
'user_location'
]);
if (result.status == LoginStatus.cancelled) return false;
if (result.status == LoginStatus.success) {
if (result.accessToken == null) {
Fluttertoast.showToast(msg: Translate.unknownError());
return false;
}
return true;
}
return false;
}
///APPLE
static Future<bool> signWithApple() async {
final AuthorizationCredentialAppleID credential = await SignInWithApple.getAppleIDCredential(
scopes: [AppleIDAuthorizationScopes.email, AppleIDAuthorizationScopes.fullName],
);
final String? email = credential.email;
final String? name = credential.givenName;
final String? famName = credential.familyName;
String? userName;
if (name != null && famName != null){
userName = '$name $famName';
}
if (email == null) {
Fluttertoast.showToast(msg: Translate.invalidEmail());
return false;
}
final bool firstLogin = await isFirstLogin(email);
print(firstLogin);
ParseResponse response;
if (firstLogin){
response = await ParseUser.loginWith(
'apple',
apple(
credential.identityToken!,
credential.userIdentifier!
),
email: email,
username: userName
);
}else {
response = await ParseUser.loginWith(
'apple',
apple(
credential.identityToken!,
credential.userIdentifier!
),
);
}
if (response.success){
return _setDeviceId(response.result);
}else {
_handleError(response);
}
_sendLoginInfo(credential, response.success);
return false;
}
您缺少的是一个名为 链接用户 的功能,我在 Flutter SDK 中没有找到内置函数,因此我附加了来自 REST API 的文档。
Parse 允许您将用户与 Twitter 和 Facebook 等服务链接起来,使您的用户能够使用其现有身份注册或登录您的应用程序。这是通过注册和更新 REST 端点来完成的,方法是在
字段中提供您希望链接到用户的服务的身份验证数据。一旦您的用户与某项服务关联,该服务的authData
将与该用户一起存储,并可通过登录进行检索。authData
您应该使用登录提供程序数据填充用户的
authData
属性,以便将其链接到用户。这可以借助云代码或来自客户端的简单 http 请求来完成。假设您将把它集成到您的 flutter 应用程序中,链接 facebook 登录提供程序的请求应由以下内容组成:
# Use PUT http method
curl -X PUT \
# Add App ID to header
-H "X-Parse-Application-Id: ${APPLICATION_ID}" \
# Add Client Key to header
-H "X-Parse-Client-Key: ${CLIENT_KEY}" \
# Add Session Token to header of the user you're linking the login provider to
-H "X-Parse-Session-Token: r:samplei3l83eerhnln0ecxgy5" \
# Add Contet-Type to header
-H "Content-Type: application/json" \
-d '{
"authData": {
"facebook": {
"id": "123456789",
"access_token": "SaMpLeAAibS7Q55FSzcERWIEmzn6rosftAr7pmDME10008bWgyZAmv7mziwfacNOhWkgxDaBf8a2a2FCc9Hbk9wAsqLYZBLR995wxBvSGNoTrEaL",
"expiration_date": "2022-01-01T12:23:45.678Z"
}
}
}' \
# Call endpoint for the user you're linking the login provider to
https://YOUR.PARSE-SERVER.HERE/parse/users/{user_object_id}