urish/firebase-server

firebase-admin

Closed this issue · 12 comments

Gabrn commented

Is it possible to use firebase-admin with firebase-server and if it is what credentials do I pass?

Any word on this? I've been wondering the same thing.

Gabrn commented

I managed to make it work but I cheated a little.

This is how I initialized firebaseAdmin:

const config = {
    databaseURL: 'ws://127.0.1:5000', // ws://127.0.1 is localhost (you can also change the private/etc/hosts file)
    credential: {
      getAccessToken: () => ({
        expires_in: 0,
        access_token: '',
      }),
    },
  };

app = firebaseAdmin.initializeApp(config);

This is enough to make firebase admin work if you don't want to check any rules and don't upload any rule file.

Now if you want use firebase rules you need to do a bit more. I have a fork of this repository where I made this work and also let users who signInWithCustomToken in.. I hacked a bit around things.
This is the repository https://github.com/GabiNir/firebase-server
And you will also need to stub firebaseAdmin auth() function like this:

import jwt from 'jsonwebtoken'

const originalAuthFunction = firebaseAdmin.auth;
  firebaseAdmin.auth = () => {
    const auth = originalAuthFunction();
    auth.createCustomToken = (uid, developerClaims) => {
      const token = jwt.sign({ uid, claims: developerClaims }, serviceAccount.private_key, {
        audience: 'https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit',
        expiresIn: 60 * 60,
        issuer: serviceAccount.client_email,
        subject: serviceAccount.client_email,
        algorithm: 'RS256',
      });

      return Promise.resolve(token);
    };

    return auth;
  };

@GabiNir, any chance you know how to make this mock credential in an iOS app using Firebase?

Gabrn commented

@genkobar sorry but I don't really follow, are you trying to use firebase server in an iOS app?

@GabiNir Oh, sorry, I conflated this FirebaseAdmin project with the official Firebase library FirebaseAuth. I'm trying test an iOS app against firebase-server instead of actual Firebase, but I was having trouble with tests that require an authenticated user.

Gabrn commented

@genkobar you can see my previous post about how to work with firebase-server and authenticated users (it doesn't work properly out of the box because for example authenticateWithCustomToken is forcing you to use RS256 and not HS256 which is used by firebase-server).

Another thing you might want to do is download your rule file every time you run your tests to keep it in track with how the rules are in your production and not run out of sync. Here is an example:

const server = new FirebaseServer(5000, 'localhost.firebaseio.test');
    const rules = (await axios.get(
      'https://<firebase_proj>.firebaseio.com/.settings/rules.json?auth=<your_auth>'
    )).data;
    server.setRules(rules);

Using @GabiNir's method, I seem to be getting this error:

     Error: Invalid value for config firebase.databaseURL: ws://127.0.1:5000

Anyone know a solution to hack around this?

Gabrn commented

@calclavia You can set an alias in etc/hosts in your local computer for localhost as local.firebaseio.test
and then just use this string like this: local.firebaseio.test:5000

@GabiNir Is there a way to do that without hosts (so this can be easily run on a CI)? Seems like there's a way to hack the websocket and trick it to connect to localhost, but I haven't got that working with firebase-admin...

var websocketMock = _.defaults({

@calclavia Not that it necessarily resolves your problem, but as far as I know, all of the most popular SaaS CI solutions allow you to configure your etc/hosts fairly easily. Some docs that might help:

Circle CI
Travis CI

urish commented

@calclavia see this: firebase/firebase-js-sdk#426, seems like you will just be able to use ws://localhost soon

urish commented

also, regarding the original question - see #81