It’s easy enough to get facebook and google signin working with react-redux-firebase. What isn’t as well supported, is upgrading anonymous accounts through facebook and google signin.
Here is what we’ll be building (Prateek built this ui)
Preparation
Accounts and stuff
Activate google and facebook login in the Firebase console. You’ll need to create a Facebook developer account as well and activate Facebook login. Make sure to add your client id and secret to Firebase console.
Dependencies
We will be using react-google-signin and react-facebook-signin. They help to abstract away some of the boilerplate code.
Coding
Google Signin
react-google-signin provides a ui element, but we weren’t especially happy with it and opted for rendering our own.
<GoogleLoginclientId="Your Google clientId"render={renderProps=>(<Buttonvariant="contained"color="inherit"startIcon={<imgstyle={{height:"30px"}}src={google_icon}alt=""/>}size="small"fullWidthonClick={renderProps.onClick}style={{height:"100%"}}>SigninwithGoogle</Button>)}buttonText="Login"onSuccess={this.googleSignin}onFailure={this.googleSigninFailure}isSignedIn={true}cookiePolicy={'single_host_origin'}/>
Now is where the “onSuccess” callback becomes important. We want to make sure that it successfully links the current anonymous user with the account that is signing in. We take care of this as follows:
The idea is that we use the token returned by the google signin to create a firebase credential, and then link it to the anonymous user. This basically creates the new user with the same uid as the anonymous user.
Finally we login with the credential inorder to set the local state correctly.
And facebookSignin. Notice that I am using event.accessToken instead of event.authResponse.accessToken like in the firebase docs because the response type that react-facebook-login passes to the event handler is not the same as that of the approach the firebase docs assume that you will be using.