Skip to content
This repository has been archived by the owner on Mar 29, 2022. It is now read-only.

firebase-admin #94

Closed
Gabrn opened this issue Jun 19, 2017 · 12 comments
Closed

firebase-admin #94

Gabrn opened this issue Jun 19, 2017 · 12 comments

Comments

@Gabrn
Copy link

Gabrn commented Jun 19, 2017

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

@azaslavsky
Copy link

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

@Gabrn
Copy link
Author

Gabrn commented Jul 12, 2017

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;
  };

@genkobar
Copy link

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

@Gabrn
Copy link
Author

Gabrn commented Jul 14, 2017

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

@genkobar
Copy link

@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
Copy link
Author

Gabrn commented Jul 16, 2017

@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);

@calclavia
Copy link

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
Copy link
Author

Gabrn commented Sep 7, 2017

@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

@calclavia
Copy link

@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({

@azaslavsky
Copy link

@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
Copy link
Owner

urish commented Jan 12, 2018

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

@urish urish closed this as completed Jan 12, 2018
@urish
Copy link
Owner

urish commented Jan 12, 2018

also, regarding the original question - see #81

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants