我在RN应用中创建了一个Facebook登录身份验证系统。我在此过程中使用了以下功能:
检查令牌是否存在,如果不存在,请登录-
export const facebookLogin = () => async dispatch => {
let token = await AsyncStorage.getItem('fb_token');
console.log("got token", token);
if (token) {
dispatch({ type: FACEBOOK_LOGIN_SUCCESS, payload: token});
console.log('facebook login success!');
} else {
doFacebookLogin(dispatch);
console.log('need to login');
}
doFacebookLogin应该从Facebook返回一个令牌,然后从Firebase身份验证获取uid,然后仅将uid登录到我的数据库中(在createUser()
中)。然后调用tryFacebookGetToken-
export const doFacebookLogin = async dispatch => {
let {type, token} = await Facebook.logInWithReadPermissionsAsync('*** ', {
permissions: ['public_profile','email']
});
if (type === 'cancel'){
Alert.alert("There is a problem logging you with facebook. Please try again");
return dispatch({ type: FACEBOOK_LOGIN_FAIL })
} else {
await AsyncStorage.setItem('fb_token', token);
const credential = firebase.auth.FacebookAuthProvider.credential(token);
await firebase
.auth()
.signInAndRetrieveDataWithCredential(credential);
firebase.auth().onAuthStateChanged((user) => {
console.log("current user is ", user.uid);
AsyncStorage.setItem('my_uid', user.uid);
dispatch(createUser(user.uid));
dispatch({ type: SAVE_USER, payload: {prop: "uid", value: user.uid}});
dispatch(tryGetFacebookToken(user.uid));
});
}
};
tryGetFacebookToken()
获取令牌,然后使用它来检索用户凭据(例如用户名和FB个人资料图片),然后将此数据存储在新创建的
export const tryGetFacebookToken = (user_id) => async dispatch => {
let token = await AsyncStorage.getItem('fb_token');
console.log("got token", token);
axios.post(`https://graph.facebook.com/me?access_token=${token}&fields=id,name,birthday,link,email,gender,picture.height(200)`)
.then(function (response) {
const { id, picture, name, email, gender, link, birthday} = response.data;
const url = picture.data.url;
console.log(`my user facebook id ${id} \n picture ${url} \n name ${name} \n link ${link} \n gender ${gender} \n email ${email} \n birthday ${birthday}`);
if (token) {
AsyncStorage.setItem('my_url', url);
AsyncStorage.setItem('my_name', name);
dispatch({ type: TOKEN_RECEIVED, payload: token});
dispatch({type: SAVE_USER, payload: {prop: "fbPicture", value: url}});
dispatch({type: SAVE_USER, payload: {prop: "name", value: name}});
firebase.database().ref(`/users/${user_id}/`).set({name,photo: url}); //add then statement
dispatch(updateUserData(user_id,name,url,link, email));
} else {
console.log("tryGetFacebookToken did not retrieve token");
}
})
.catch((error) => {Alert.alert(error);});
}
现在解决这个问题-我已经有大约600个用户,而其中只有15个用户,由于未知原因,我没有用户名,电子邮件或FB个人资料图片。此外,在Firebase身份验证中,我确实看到了他们的facebook电子邮件标识符。 发生的另一件事是,他们以某种方式转到下一页并输入他们的电话号码,除非成功登录Facebook,否则这是不会发生的。
我的猜测是,他们以某种方式在登录时删除了所需的FB数据,但我无法复制该数据。