Ad

Firebase Authentication With Custom Backend

I plan to use Firebase for authentication for my iOS app. But I want to use custom backend for rest of the REST APIs. How can I add authorization for users authenticated with Firebase in my custom backend ? Can we use both custom backend and firebase authentication? How do I maintain the session using both Firebase and custom backend?

Ad

Answer

You can verify the token on your backend server with the firebase Admin SDK.

So on the app you get a firebase access token and send this to your server. On IOS you do:

FIRUser *currentUser = [FIRAuth auth].currentUser;
[currentUser getIDTokenForcingRefresh:YES
                       completion:^(NSString *_Nullable idToken,
                                    NSError *_Nullable error) {
      if (error) {
        // Handle error
        return;
      }

      // Send token to your backend via HTTPS
      // ...
  }];

More Info here: https://firebase.google.com/docs/auth/admin/verify-id-tokens#retrieve_id_tokens_on_clients

On the server you do:

admin.auth().verifyIdToken(idToken)
.then(function(decodedToken) {
  var uid = decodedToken.uid;
  // ...
}).catch(function(error) {
  // Handle error
});

As you can see you even get the "uid" of the user. More Infos here: https://firebase.google.com/docs/auth/admin/verify-id-tokens

Ad
source: stackoverflow.com
Ad