2017-11-03 229 views
0

我需要处理react-native中的Facebook登录错误。React Native FB SDK处理验证拒绝

尝试登录后重新安装我的应用程序的显示弹出式用户:

"Facebook. Application would like to access your basic profile info and list of friends."

两个选项:“不允许”和“OK”。正常的流量,我得到一个适当的令牌。但是,如果用户在onLoginFinished拒绝我得到以下结果:

{ 
    "isCancelled": true, 
    "grantedPermissions": null, 
    "declinedPermissions": null 
} 

其中有没有关于它为什么被取消的信息。拒绝是唯一的理由"isCancelled": true'system_account'

之后,所有的连续尝试与'system_account'登录立即失败,并出现以下错误:

{ 
    "code": "FacebookSDK", 
    "domain": "com.facebook.sdk.login", 
    "framesToPop": 1, 
    "nativeStackIOS": [...], 
    "userInfo": { 
    "NSLocalizedDescription": "Access has not been granted to the Facebook account. Verify device settings.", 
    "com.facebook.sdk:FBSDKErrorLocalizedDescriptionKey": "Access has not been granted to the Facebook account. Verify device settings." 
    } 
} 

它给我,我可以展示给用户的错误,但被本地化我不能可靠地解析它。所以问题是如何可靠地检测用户何时不授予权限?

的代码,我有:

import React from 'react' 
import {AccessToken, LoginManager} from 'react-native-fbsdk' 

class Login extends React.Component { 
    login =()=>{ 
     LoginManager.setLoginBehavior('system_account') 
     LoginManager.logInWithReadPermissions() 
      .then(this.onLoginFinished) 
      .catch(this.onLoginError) 
    } 
    onLoginFinished = (result)=>{ 
     if(result.isCancelled) { 
      // 
     } else { 
      AccessToken.getCurrentAccessToken().then((data)=>{ 
       this.props.onLogin(data.accessToken.toString()) 
      }) 
     } 
    } 
    onLoginError = (error)=>{ 
     // Want to handle this case but 
     //can only show a message to the user =(
    } 
    render() { 
     return (
      <View> 
       <TouchableHighlight onPress={this.login}> 
        <View><Text>Facebook</Text></View> 
       </TouchableHighlight> 
      </View> 
     ) 
    } 
} 

从我能想到的我只能接受与'system_account'取消和单独处理其行为的唯一途径后使切换到'native'登录行为?

回答

2

使用Facebook登录时,有几种情况需要处理。授予的所有权限,并记录在

  • 用户授予某些权限,并记录在
  • 用户没有授予任何权限,并记录在
  • 用户取消登录尝试
  • 如果

    1. 用户你检查LoginManager.logInWithReadPermissions它使用iOS SDK的本机代码(iOS)FBSDKLoginManagerLoginResult。在documentation of iOS SDKisCancelled是一个BOOL,它显示“登录是否被用户取消”。

      因此,如果在您的返回结果isCanceled为真,则表示用户手动取消了登录请求。这可能是因为用户改变了他/她的想法或错误点击了按钮,或者仅仅是因为当他/她看到您要求的权限列表等时他改变了主意。当您检查并看到isCanceled值是真的,那么你可以假设用户不想用Facebook登录,你可以像这样继续(重定向到另一种登录方法或显示用户应该登录的弹出窗口等)

      如果用户完成Facebook登录,但未选中某些权限请求(如生日或喜欢等),则权限将显示在declinedPermissions中。您可以查看此属性的lenght,并按照您的愿望进行处理。您可以创建一个对象,其中包含在任何权限被拒绝时需要显示的所有错误消息。

      const permissions = [ 
          { 
          permissionName: 'user_likes', 
          errorMessage: 'To give you better results we need your likes' 
          }, 
          { 
          permissionName: 'user_birthday', 
          errorMessage: 'Would you like to receive a gift from us on your birthday?' 
          }, 
          { 
          permissionName: 'user_friends', 
          errorMessage: 'We can\'t match you without knowing your friends' 
          } 
      ]; 
      
      
      
      // .... 
      login =()=>{ 
          LoginManager.setLoginBehavior('system_account') 
          LoginManager.logInWithReadPermissions(permissions.map((permission) => (permission.permissionName))) 
           .then(this.onLoginFinished) 
           .catch(this.onLoginError) 
      } 
      
      onLoginFinished = (result)=>{ 
          if(result.isCancelled) { 
           // user canceled login manually 
          } else { 
           if (result.declinedPermissions && result.declinedPermissions.lenght > 0) { 
            // handle declined permissions 
            result.declinedPermissions.forEach((declinedPermission) => { 
            // match the declinedPermission with the permission object 
            } 
           } else { 
            // user gave all the permissions 
           } 
           AccessToken.getCurrentAccessToken().then((data)=>{ 
            this.props.onLogin(data.accessToken.toString()) 
           }) 
          } 
      } 
      
    相关问题