Firebase Authentication with Realm - token has invalid signature

cloud

#1

I’m trying to follow the firebase authentication guide but I’m hitting a wall in the last step, when I try to open realm with the JWT.

My firebase function is identical to the example.

My didSignIn function is as follows. The error occurs at the realm login step; I never get a sync user but an error:0906D06C:PEM routines:PEM_read_bio:no start line. I think the token returned from firebase might be bad because https://jwt.io says it has an invalid signature.

func authUI(_ authUI: FUIAuth, didSignInWith user: User?, error: Error?) {
        if let fireUser = user {
            print("GOT FIRE USER")
            
            Functions.functions().httpsCallable("myAuthFunction").call() { (result, error) in
                if let error = error as NSError? {
                    if error.domain == FunctionsErrorDomain {
                        //                    let code = FunctionsErrorCode(rawValue: error.code)
                        //                    let message = error.localizedDescription
                        //                    let details = error.userInfo[FunctionsErrorDetailsKey]
                    }
                    // ...
                }
                if let token = (result?.data as? [String: Any])?["token"] as? String {
                    print("GOT TOKEN! \(token)")
                    let creds = SyncCredentials.jwt(token)
                    SyncUser.logIn(with: creds, server: YouModel.AUTH_URL, onCompletion: { (rlmUser, err) in
                        if let syncUser = rlmUser {
                            print("got a syncuser: \(syncUser.identity)")
                            self.mainRealm = self.openMainRealm(syncUser: syncUser)
                        } else if let error = err {
                            print("err opening realm")
                            fatalError(error.localizedDescription)
                        }
                    })
                }
            }
        }
    }

I think the problem might be with my key pair. I generate with

ssh-keygen -t rsa -b 4096 -m PEM -f key

then copy the base64 portion of key.pub to my realm cloud settings like so

-----BEGIN PUBLIC KEY-----
key
-----END PUBLIC KEY-----

#2

Solved, I found other documentation for generating the keys: https://github.com/realm/realm-sync-samples/blob/master/05-jwt-authentication/server/keygen.sh

Maybe this should be inserted into the firebase authentication guide, rather than the link to ssh.com?


#3

I am in desperate need of some help regarding Firebase Auth. Any chance you would paste your Google Cloud function here? I tried using the code in the Realm Firebase Auth guide you linked above but I think I’m having an issue with the pathToMyPrivateKeyFile. Any extra guidance would be greatly appreciated.


#4

My function is same as shown in the docs. Open your private key file in Node like this:

const key = fs.readFileSync('private.pem');

#5

I am new to JavaScript and Node.Js so I thank you for your patience in advance. This is my first time dealing with JWT. Where do I store this private key file? Is the ‘private.pem’ file store in your project? If I can get this figured out, I am going to write a detailed walk through of this entire process for the community.


#6

I guess go through the firebase functions “hello world” tutorial, and maybe a brief intro to Node.js. That will give you a better outlook than asking for help every step of the way. The short answer is the .pem goes in the same directory as your functions file, for that code I gave.


#7

Great, that is helpful. Thank you for your help :grinning:


#8

@harrynetzer I think where @joshRond is stuck is where to put the file since this is a cloud function. Could you post your project structure so he can see where your private.pem file is in relation to your other files?