Firebase Login Authentication in Java

Posted By : Himani Mishra | 04-Mar-2021

Introduction: The Firebase Authentication provides methods to create and manage users that use their email addresses and passwords or federated identity provider or phone number to sign in.

Federated Identity Provider: Federated identity providers like Google, Facebook, Apple, GitHub, and Twitter, etc.

 

 

Working: To sign a user into an app  we can use the user's email address and password or select any of the federated identity providers then we will get an authentication token from firebase SDK on client-side now we can send this token to our own backend server and authenticate it

 

Also Read: Creating Asynchronous Methods in Spring Boot

 

Backend side Implementation :

 

1. Add the firebase admin SDK

Before this, we need the following

A Firebase project
A service account to communicate with Firebase
A configuration file with the service account's credentials

 

If you do not have a Firebase project, you need to create one by visiting the Firebase Console.


Add the SDK

 dependencies {
  implementation 'com.google.firebase:firebase-admin:7.1.0'
}

or

<dependency>
  <groupId>com.google.firebase</groupId>
  <artifactId>firebase-admin</artifactId>
  <version>7.1.0</version>
</dependency>


Also Read: 10 points for writing and maintaining APIs

 

2. Initialize the SDK

To generate a private key file :
1. Go to Firebase Console and open Setting>Service Accounts
2. Click Generate New Private Key
3. Store the JSON file containing private key

application.properties file 

     app.firebase-config="path of service-account-file.json"

FirebaseConfiguration.java file

public class FirebaseConfiguration {

    @Value("${app.firebase-config}")
    private String firebaseConfig;

    private void initialize() throws IOException {

          FirebaseOptions options = new FirebaseOptions.Builder()
                                   .setCredentials( GoogleCredentials.fromStream( new ClassPathResource( firebaseConfig ).getInputStream() ) )
                                   .build();

          if ( FirebaseApp.getApps().isEmpty() )
            {
               this.firebaseApp = FirebaseApp.initializeApp( options );
            }
            else
            {
                this.firebaseApp = FirebaseApp.getInstance();
            }

   }

}

 

Also Read: Rest Template in Spring Boot

 

3. Verify Id tokens 

// idToken comes from the client app
FirebaseToken decodedToken = FirebaseAuth.getInstance().verifyIdToken(idToken);
String uid = decodedToken.getUid();
 

Why You Should Choose Oodles For SaaS Product Development?
 

We are a 360-degree software development company that provides cross-platform SaaS app development services to address varied software project requirements. We have an experienced team of Java, PHP, and Python developers who use advanced frameworks, tools, and SDKs to build scalable web and mobile applications with custom features. For more detail, reach us out at [email protected].

About Author

Author Image
Himani Mishra

She is a backend developer with good knowledge of various technologies. She is always willing to learn new technologies and is a good learner.

Request for Proposal

Name is required

Comment is required

Sending message..