Vineel
Vineel

Reputation: 1788

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?

Upvotes: 3

Views: 4881

Answers (1)

DominikHelps
DominikHelps

Reputation: 1021

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

Upvotes: 3

Related Questions