Custom Firebase user identity not using the Firebase user uid

两盒软妹~` 提交于 2019-12-13 17:18:19

问题


I´m playing around with the FirebaseUI-Android and have a question about what ID to use when uniquely identify users. The FirebaseUI managed the authentication right and return the IdpResponse object. This can be e.g. Facebook, Twitter, Phone and more.. This is grate thanks everyone behind this.

Because the FirebaseUser.getUid() can change when user delete/recreate his account therefore i don't want to link my users data with the FirebaseUser.getUid() token.

In the future if I decide to let user wipe there account the uid will change when user come back and all user history for that user in my system is not accessible anymore right. Also in the future if I migrate my system to someplace the uid will not be that handy to have around right?

Now I created this wrapper(code below) to create a unique ID extrapolated from the data inside the IdpResponse. Dunno really but I figure doing it this way there will never be a "id" collision unless there´s a Google2.0 e.g. Twitter2.0 :) right. And at the same time it´s easier to debunk bugs in the system because id´s are not UUID.

Is this a recommended way to handle the user id problem. I really need some feedback and pitfalls warnings about this.

@Override
public String getUserId() {
    String userId;
    FirebaseUser user = getInstance().getCurrentUser();
    if (user.getEmail() != null)
        // User sign in with E-Mail
        userId = user.getEmail().replace(".", ",");
    else if (user.getPhoneNumber() != null){
        // User sign in with Phone
        userId = user.getPhoneNumber();
    }else
        // User sign in with Twitter or Facebook 
        userId = user.getUid();
    return userId;
}

What bother me most with this is the Twitter or Facebook since I still have to use the FirebaseUser.getUid(). Is the IdpResponse.getIdpToken() better to us?


回答1:


As you mentioned in your post, using the uid is not the best option. If the user removes his account and than tries to create another account, another uid is generated. So in this case, he losses all data history. The best way to identify users, is to use the email address. The email address is unique, easy to use and if want in the future, to link a Google account with a Facebook account and with a Twitter account, based on the email address, you'll be able to do id. Because Firebase does not accept . symbol in the key, i suggest you encode the email address like this:

name@email.com -> name@email,com

As you probably see, i have changed the the dot symbol . with a coma ,. To achive this i recomand you using this methods:

static String encodeUserEmail(String userEmail) {
    return userEmail.replace(".", ",");
}

static String decodeUserEmail(String userEmail) {
    return userEmail.replace(",", ".");
}

Hope it helps.



来源:https://stackoverflow.com/questions/44540920/custom-firebase-user-identity-not-using-the-firebase-user-uid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!