Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 637
Vineel Avatar asked May 12 '26 22:05

Vineel


1 Answers

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

like image 193
DominikHelps Avatar answered May 14 '26 12:05

DominikHelps