2017-03-02 8 views
0

Ich versuche, eine Facebook-Login zu implementieren unter Verwendung reagieren-native und redux aber ich bin Gesicht auf ein Problem:Reagieren india fbsdk redux authToken undefined

enter image description here

In meiner Konsole, ich habe alle Informationen über die Benutzer, sondern in dem Objekt für redux die authToken ist nicht definiert, und ich verstehe nicht, warum .. Hier ist mein Code

app/src/facebook.js

import { 
    LoginManager, 
    AccessToken, 
    GraphRequest, 
    GraphRequestManager, 
} from 'react-native-fbsdk'; 

const facebookParams = 'id,name,email,picture.width(100).height(100)'; 

export function facebookLoginAPI() { 
    return new Promise((resolve, reject) => { 
    LoginManager.logInWithReadPermissions(['public_profile', 'user_friends', 'email']) 
    .then((FBloginResult) => { 
     if (FBloginResult.isCancelled) { 
     throw new Error('Login cancelled'); 
     } 

     if (FBloginResult.deniedPermissions) { 
     throw new Error('We need the requested permissions'); 
     } 

     return AccessToken.getCurrentAccessToken(); 
     console.log(FBloginResult); 
    }) 
    .then((result) => { 
     resolve(result); 
     console.log(result); 
    }) 
    .catch((error) => { 
     reject(error); 
     console.log(error); 
    }); 
    }); 
} 

export function getFacebookInfoAPI() { 
    return new Promise((resolve, reject) => { 
    const profileInfoCallback = (error, profileInfo) => { 
     if (error) reject(error); 

     resolve(profileInfo); 
    }; 

    const profileInfoRequest = 
     new GraphRequest(
     '/me', 
     { 
      parameters: { 
      fields: { 
       string: facebookParams, 
      }, 
      }, 
     }, 
     profileInfoCallback 
    ); 

    new GraphRequestManager().addRequest(profileInfoRequest).start(); 
    }); 
} 

export function getFacebookFriends() { 
    return new Promise((resolve, reject) => { 
    const profileInfoCallback = (error, profileInfo) => { 
     if (error) reject(error); 
     console.log(profileInfo); 
     resolve(profileInfo); 
    }; 

    const profileFriendsRequest = 
     new GraphRequest(
     '/me/friends', 
     { 
      parameters: { 
      fields: { 
       string: facebookParams, 
      }, 
      }, 
     }, 
     profileInfoCallback 
    ); 

    new GraphRequestManager().addRequest(profileFriendsRequest).start(); 
    }); 
} 

die Aktion (mit allen Aktionstypen in einer anderen Datei)

import { facebookLoginAPI, getFacebookInfoAPI } from '../src/facebook'; 
import { getServerAuthToken } from '../src/auth'; 
import { 
    AUTH_STARTED, 
    AUTH_SUCCESS, 
    AUTH_FAILURE, 
    AUTH_ERROR, 
    AUTH_FAILURE_REMOVE, 
    LOGOUT 
} from './types'; 

export function authStarted() { 
    return { 
    type: AUTH_STARTED, 
    }; 
} 

export function authSuccess(facebookToken, facebookProfile, serverAuthToken){ 
    return { 
    type: AUTH_SUCCESS, 
    facebookToken, 
    facebookProfile, 
    authToken: serverAuthToken, 
    }; 
} 

export function authFailure(authError){ 
    return { 
    type: AUTH_FAILURE, 
    authError, 
    }; 
} 

export function authFailureRemove() { 
    return { 
    type: AUTH_FAILURE_REMOVE, 
    }; 
} 

export function logout() { 
    return { 
    type: LOGOUT, 
    }; 
} 

export function facebookLogin() { 
    return (dispatch) => { 
    dispatch(authStarted()); 
    const successValues = []; 
    facebookLoginAPI() 
    .then((facebookAuthResult) => { 
     [...successValues, ...facebookAuthResult.accessToken]; 
     return getFacebookInfoAPI(facebookAuthResult.accessToken); 
    }).then((facebookProfile) => { 
     [...successValues, ...facebookProfile]; 
     return getServerAuthToken(); 
    }).then((serverAuthToken) => { 
     [...successValues, ...serverAuthToken]; 
     dispatch(authSuccess(...successValues)); 
    }).catch((error) => { 
     dispatch(authFailure(error)); 
     setTimeout(() => { 
     dispatch(authFailureRemove()); 
     }, 4000); 
    }); 
    }; 
} 

und das Reduktions:

import { 
    AUTH_SUCCESS, 
    AUTH_FAILURE, 
    AUTH_STARTED, 
    AUTH_ERROR, 
    AUTH_FAILURE_REMOVE, 
    LOGOUT 
} from '../actions/types'; 

const initialState = { 
    authenticating: false, 
    authToken: null, 
    authError: null, 
    facebookToken: null, 
    facebookProfile: null, 
} 

function authReducer(state = initialState, action) { 
    switch(action.type) { 
    case AUTH_STARTED: 
     return Object.assign({}, state, { 
     authenticating: true, 
     loginText: 'Connexion..' 
     }); 
    case AUTH_SUCCESS: 
     return Object.assign({}, state, { 
     authenticating: false, 
     authToken: action.authToken, 
     facebookToken: action.facebookToken, 
     facebookProfile: action.facebookProfile, 
     }); 
    case AUTH_FAILURE: 
     return Object.assign({}, state, { 
     authenticating: false, 
     authError: action.authError.message, 
     }); 
    case AUTH_FAILURE_REMOVE: 
     return Object.assign({}, state, { 
     authError: null, 
     }); 
    case LOGOUT: 
     return Object.assign({}, state, { 
     authenticating: false, 
     authToken: null, 
     facebookToken: null, 
     facebookProfile: null, 
     loginText: null, 
     }); 
    default: 
     return state; 
    } 
} 

export default authReducer; 

ich verstehen müssen, was die authToken ist, und warum ist er undefiniert in meinem Fall? Ist die Auth ist Erfolg .. Ich weiß es nicht!

Vielen Dank!

Antwort

1

folgenden Code aussehen wenig fischig mir

export function facebookLogin() { 
    return (dispatch) => { 
    dispatch(authStarted()); 
    const successValues = []; 
    facebookLoginAPI() 
    .then((facebookAuthResult) => { 
     [...successValues, ...facebookAuthResult.accessToken]; //remove this line 
     return getFacebookInfoAPI(facebookAuthResult.accessToken); 
    }).then((facebookProfile) => { 
     [...successValues, ...facebookProfile]; //remove this seems of no use 
     return getServerAuthToken(); //I think you may need to pass something here 
    }).then((serverAuthToken) => { 
     [...successValues, ...serverAuthToken]; //pass this value in authSuccess below instead of ...successValues (it may still be []) 
     dispatch(authSuccess(...successValues)); 
    }).catch((error) => { 
     dispatch(authFailure(error)); 
     setTimeout(() => { 
     dispatch(authFailureRemove()); 
     }, 4000); 
    }); 
    }; 
} 
+0

für die Zeit der Antwort Entschuldigung! Danke, Kumpel, es funktioniert gut! –