This post describes how to tie multiple accounts to a single $uid in a users collection.
Here's those security rules:
"rules": {
"users": {
"$uid": {
".write": "auth != null &&
(data.val() === null ||
(auth.provider === 'facebook' && auth.id === data.child('facebookUid').val()) ||
(auth.provider === 'twitter' && auth.id === data.child('twitterUid').val()))"
}
},
"usersMap": {
"facebook": {
"$fuid": {
".read": "auth != null && auth.provider === 'facebook' && auth.id === $fuid",
".write": "auth != null &&
(data.val() === null ||
root.child('users').child(data.val()).child('facebookUid').val() == auth.id)"
}
},
"twitter": {
"$tuid": {
".read": "auth != null && auth.provider === 'twitter' && auth.id === $tuid",
".write": "auth != null &&
(data.val() === null ||
root.child('users').child(data.val()).child('twitterUid').val() == auth.id)"
}
}
}
}
Here is how I imagine a practical way to put these rules to use:
A user "Logs in" with their Facebook account.
Does the
$fuidexist? If not add a new$uidtousers. In the success callback create a$fuidunderuserMap/facebookwith a property value of$fuid.uidequal too$uid.If it does exist just ignore the request and return a message like "User already exists".
But what if a user wants to tie another account to the same master $uid?
Let's say the user is still logged in with their Facebook account and wants to add their Twitter account. Let's roll through that workflow again...
User logs in with another account.
Does the
$tuidexist? No but if theauthobject is holding both the Facebook and the Twitter sessions then we don't want to create another$uid- instead we want to map the$tuidto the same$uidthe$fuidis mapped too.Does the
authobject have support for accessing properties of simultaneous authentication objects? For example if we were logged in with both Facebook and Twitterauth.idwould be different for both right?Am I thinking about this the wrong way? How is it possible to map additional accounts to
$uidusing the security rules above?