I'm working with firebase and angularfire2
which allows authentication with Facebook, Google+ and Twitter.
Now when a user authenticates like this, I get a response looking like the following.
{
"uid":"someUID",
"displayName":"ivaro18",
"photoURL":"https://lh3.googleusercontent.com/-myPicture/photo.jpg",
"email":"myEmail@gmail.com",
"emailVerified":true,
"isAnonymous":false,
"apiKey":"aUUIDApiKey",
"appName":"[DEFAULT]",
"authDomain":"mydemo.firebase.com",
"stsTokenManager":{
"apiKey":"aUUIDApiKey",
"refreshToken":"AHugeRefreshToken",
"accessToken":"aHugeAccessToken",
"expirationTime":1481297860666
},
"redirectEventId":null
}
The authentication works and the user data can be retrieved (left out providerData
because it wasn't usefull in the JSON - won't be used)
I will send this uid
or apiKey
to my REST API (via Angular2's http.post
), but to prevent man-in-the-middle attacks I want to validate the token.
So (edit: added the if
and isComplete
@JvmStatic fun main(args: Array<String>) {
println("yes")
val options = FirebaseOptions.Builder()
.setServiceAccount(
FileInputStream("C:/Users/ivaro18/firebase.json")
).setDatabaseUrl("https://myDemo.firebaseio.com")
.build()
FirebaseApp.initializeApp(options)
if(
FirebaseAuth.getInstance().verifyIdToken("whatToken?")
.addOnSuccessListener(OnSuccessListener<FirebaseToken>() {
@Override
fun onSuccess(decodedToken: FirebaseToken) {
val uid = decodedToken.getUid()
println("UID:::" + uid)
}
})
.addOnFailureListener(OnFailureListener() {
@Override
fun onFailure(e: Exception) {
e.printStackTrace()
}
})
.addOnCompleteListener(OnCompleteListener<FirebaseToken>() {
@Override
fun onComplete(token: FirebaseToken) {
println("Token: " + token)
}
}).isComplete()) {
println("verification completed");
}
println("ok")
}
But this code outputs (Kotlin btw, almost same syntax as Java and it can run Java code)
yes
verficiation completed
ok
and then terminates after a while. I'd expect one of the listeners to fire? What is wrong with the code?
And what ID from that JSON array should I be validating within the FirebaseAuth
?
edit
The uid
part of the JSON is the same UID mentioned in the Firebase Authentication page (shown below)
I want to check if the UID
is equal to the UID
on the console. (preferrably by sending the apiKey
and accessToken
but just checking if the uid
is the same would be sufficient for now.