I have written a basic app using Java, Google Web Toolkit and Google Cloud Datastore. For authentication I am using Firebase. When the front end makes a RPC call it passes the user token so that the backend can validate it.
I would like to create a VerifyToken class in the backend, which will receive a user token, call the Firebase verifyIdToken to verify it, then return the user uid or 0 if the user token has not been successfully verified. The class which receives the RPC call will then use the uid to get data and return it in the RPC response.
Here's the current code:
public class VerifyToken
{
public String verify(String token)
{
String uid = "0";
try
{
//Connect to Firebase
FirebaseOptions options = new FirebaseOptions.Builder()
.setServiceAccount(new FileInputStream("firebaseJsonHere"))
.setDatabaseUrl("dbUrlHere")
.build();
FirebaseApp.initializeApp(options);
//Verify the token
FirebaseAuth.getInstance().verifyIdToken(token)
.addOnSuccessListener(new OnSuccessListener<FirebaseToken>() {
@Override
public void onSuccess(FirebaseToken decodedToken) {
String uid = decodedToken.getUid();
System.out.println("uid decoded = " + uid);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e)
{
System.out.println(e.getMessage());
}
});
}catch(Exception e){
System.out.println("Exception: " + e.getMessage());
}
//Return the uid or 0 if not validated
return uid;
}
}
Please could someone let me know how to get the result from the SuccessListener. I can see that the uid is successfully decoded, just not sure how to get the result back.
Many thanks, Ed