I am currently using Firebase Auth to authenticate all users and using mongodb realm/ realm sync/ atlas as my DB. From what I have heard, using 2 separate 'auths' (firebase and mongo) and linking them with the Firebase UID as a key seems like a bad idea. As such, JWT's seem like my best bet.
I feel as the following code is a-ok, but the config on the realm app is iffy: I keep getting that the public key is not PEM formatted when I use custom JWT token and when I use the JWK URI (https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com) I get the following error:
Login failed: failed to fetch JWK from URI: failed to construct key from keys: unsupported kty type Error Domain=realm::app::ServiceError Code=47 "failed to fetch JWK from URI: failed to construct key from keys: unsupported kty type " UserInfo={NSLocalizedDescription=failed to fetch JWK from URI: failed to construct key from keys: unsupported kty type , realm::app::ServiceError=AuthError}
// Function is supposed to auth user using mongo auth by using the firebase jwt
func authJWTMongo(completion: @escaping (Bool) -> Void) -> Void{
let currentUser = Auth.auth().currentUser
currentUser?.getIDTokenForcingRefresh(true) { idToken, error in
if error != nil {
print("Totally f'ed the mongoAuth")
completion(false)
return;
}
// Auth using Mongo shit
print("JWT is< \(String(describing: idToken))>")
if let idToken = idToken{
let credentials = Credentials.jwt(token: idToken)
app.login(credentials: credentials) { (result) in
switch result {
case .failure(let error):
print("Login failed: \(error.localizedDescription)")
print("\(error)")
completion(false)
case .success(let user):
print("Successfully logged in as user \(user)")
completion(true)
// Now logged in, do something with user
// Remember to dispatch to main if you are doing anything on the UI thread
}
}
}
}
}
Any help is appreciated.
Apparently the correct JWK URI is https://www.googleapis.com/service_accounts/v1/jwk/securetoken@system.gserviceaccount.com
Nothing wrong with the swift code above.