Tyler,
I should have thought to get that generated. Here are my findings...
The running code, the user variable is pulled from within the onAuthStateChanged callback.
firebase.auth().onAuthStateChanged((user: firebase.User) => {
if (user) {
const lastOnlinePath = `users/${user.uid}/lastOnline`;
lastOnlineRef = firebase.database().ref(lastOnlinePath);
const connectedRef = firebase.database().ref('.info/connected');
connectedRef.on('value', (snap) => {
if (snap && snap.val() === true) {
lastOnlineRef.onDisconnect().set(firebase.database.ServerValue.TIMESTAMP)
}
}
}
}
The actual log:
[2018-09-24T22:49:13.860Z] @firebase/database: p:0: reportStats {"c":{"sdk.node.5-5-0":1}}
[2018-09-24T22:49:13.861Z] @firebase/database: p:0: {"r":1,"a":"s","b":{"c":{"sdk.node.5-5-0":1}}}
[2018-09-24T22:49:13.862Z] @firebase/database: event: /.info/connected:value:true
[2018-09-24T22:49:13.863Z] @firebase/database: p:0: onDisconnect o {"p":"/users/Rodpwg0ZliSMin4ussiLOz7oRUs1/lastOnline","d":{".sv":"timestamp"}}
[2018-09-24T22:49:13.864Z] @firebase/database: p:0: {"r":2,"a":"o","b":{"p":"/users/Rodpwg0ZliSMin4ussiLOz7oRUs1/lastOnline","d":{".sv":"timestamp"}}}
[2018-09-24T22:49:13.923Z] @firebase/database: p:0: from server: {"r":1,"b":{"s":"ok","d":""}}
[2018-09-24T22:49:13.928Z] @firebase/database: c:0:0: Primary connection is healthy.
[2018-09-24T22:49:13.928Z] @firebase/database: p:0: from server: {"r":2,"b":{"s":"ok","d":""}}
[2018-09-24T22:49:14.736Z] @firebase/database: p:0: onDisconnect o {"p":"/users/Rodpwg0ZliSMin4ussiLOz7oRUs1/lastOnline","d":{".sv":"timestamp"}}
[2018-09-24T22:49:14.736Z] @firebase/database: p:0: {"r":4,"a":"o","b":{"p":"/users/Rodpwg0ZliSMin4ussiLOz7oRUs1/lastOnline","d":{".sv":"timestamp"}}}
Pretty simple code. My last unit test had this:
user.uid == Rodpwg0ZliSMin4ussiLOz7oRUs1
So far, all this looks good. However, when I run the code above as part of my unit tests, which consists of create a user, sign out, try to sign in with bad password (expect fail) and sign in with same previous user. I trimmed down the logs to the relevant ones.
In my realtime database, this is the path and value set:
/users/YVZ2q2pd8uZh7NqGZpsJeNiRZfn1/lastOnline
Also, if I were to replace the above code:
lastOnlineRef.onDisconnect().set(firebase.database.ServerValue.TIMESTAMP)
with:
lastOnlineRef.set(firebase.database.ServerValue.TIMESTAMP)
that writes exactly to the expected path.
So, yeah... I'm baffled and I wouldn't be surprised if I'm missing some nuance though. Remember, if I use a path OTHER than the current user.uid then it writes exactly where expected on that onDisconnect :/