Working with the backend with Spring (Java) and Firebase. We use Firebase tokens (added as an authentication header) to identify the user using the built-in UID .
Unfortunately, retrieving this UID from the token must be done asynchronously, so I can only get the token from the callback onSuccess. To answer this question, I have to return the object from the method below deleteUser, however I cannot know what the answer will be until I get a success / failure callback!
I can imagine a way to do this, expecting a flag that sets my callback, or with some messy time, but I wonder if there is a clean way to handle this without introducing race conditions or a lot of extra code. Can anyone help?
Display requests (processes the request, responds to requests)
@RequestMapping(value = "/users", method = RequestMethod.DELETE)
public @ResponseBody String deleteUser(@RequestHeader("Authentication") String token) {
FirebaseUtil.getUid(token, new OnSuccessListener<FirebaseToken>() {
@Override
public void onSuccess(FirebaseToken decodedToken) {
String uid = decodedToken.getUid();
}
}, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
User userToDelete = userDao.get(uid);
userDao.delete(uid);
clearUserAccounts(userToDelete);
return uid + " was deleted";
}
FirebaseUtil.getUid ()
public static void getUid(String token, OnSuccessListener<FirebaseToken> successListener, OnFailureListener failureListener) {
FirebaseAuth.getInstance()
.verifyIdToken(token)
.addOnSuccessListener(successListener)
.addOnFailureListener(failureListener);
}
source
share