2017-11-03 86 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()) 
           }) 
          } 
      } 
      
    相關問題