javascript - Linking External accounts with Firebase Auth + Firebase DB -
i use firebase auth manage users , tie them firebase db store data. when trying make updates firebse db users find myself doing things this:
const users = firebase.database().ref(`users`); let provider = new firebase.auth.googleauthprovider(); firebase.auth().currentuser.linkwithpopup(provider).then(function(result) { // accounts linked. let credential = result.credential; let user = result.user; let key = ''; let userref = ''; users.orderbychild('uid').equalto(user.uid).once('value').then(snapshot => { snapshot.foreach((child) => { key = child.key; userref = firebase.database().ref(`users/${key}`); let updateduser = {}; userref.update({ linkedaccounts: user.providerdata }); userref.once('value').then(snapshot => { updateduser = snapshot.val(); dispatch({ action: link_google, payload: updateduser }); }); return; }); }); }).catch(function(error) { // handle errors here. dispatch(autherror(error)); }); in example i'm linking new auth provider (google). on client side need disable button checks sort out whether user has data etc. i'm using react/redux call dispatch() send action , client updated new user in application's state. working (to extent), however, have couple questions...
1. in example updateduser not contain key user. meaning looks like:
{ displayname: 'frank smith', photourl: '', uid: 12345 }
... when want whole object like:
'-f12345asdfg': { displayname: 'frank smith', photourl: '', uid: 12345 }
... key included remain consistent way data returned elsewhere. know can make second fb db call user uid (like above) return way want seems inefficient...
2. is efficient/effective way this? seems messy.
thanks in advance!
Comments
Post a Comment