ThisIsRudigo / firebaseauth

Firebase authentication library - a node js wrapper around the Firebase REST API
42 stars 8 forks source link

Readme out of date. Protect has changed to Guard? #14

Closed potatowave closed 6 years ago

potatowave commented 6 years ago

I've been trying to use this dependency but have had a couple of issues setting up.

Instead of:

var firebase = new FirebaseAuth(process.env.FIREBASE_API_KEY);

I had to use

let firebase = new FirebaseAuth.default(process.env.FIREBASE_API_KEY);

I'm stuck at using protector (now Guard?) - how is this meant to be implemented? I'll report back if I figure it out.

MrStLouis commented 6 years ago

your initial issue is because you are using require instead of import which requires you to pull of the default property because it is exported as a default.

I am having the same issue in regards to Guard. It seems Guard and defaultTokenMiddleware are both hidden as static properties and not exposed

static Guard = Guard;

static defaultTokenMiddleware(serviceAccount: any): RequestHandler {
    return new Guard(serviceAccount).middleware;
}

maybe this tool isn't available for use yet? A workaround is to extend the FirebaseAuth class and add the method yourself.

export class Firebase extends FirebaseAuth {
  constructor(apiKey: string) {
    super(apiKey);
  }
  defaultTokenMiddleware(serviceAccount: ServiceAccount): RequestHandler {
    return FirebaseAuth.defaultTokenMiddleware(serviceAccount);
  }
}
const firebase = new Firebase(FIREBASE_API_KEY || 'throw error');

export { firebase };

you can now access the protector or guard by passing your serviceAccount.json in like so:

const serviceAccount = require('../config/firebase.json');
const protector: RequestHandler = firebase.defaultTokenMiddleware(serviceAccount);

export { protector };

and use that protector as express middleware. protector checks for the token received from logging in

req.headers[this.options.tokenField] || req.body[this.options.tokenField] || req.query[this.options.tokenField];

where this.options.tokenField = 'token'

There is a very good chance parts of this setup will change as this repo matures but using the exports around your app shouldn't change. This should be enough to get you going. I have tested a login, register, and protected route using this method. Also mind you I'm using typescript so you can convert this into regular javascript but the rest remains the same

itswisdomagain commented 6 years ago

Hi. My apologies for the mix up. I recently migrated the project to typescript and haven't had the time to update the readme.

I've done that now. I hope you'll find the readme clearer and more detailed, especially wrt to the resource protection middleware.

If you encounter any new challenge, please open another issue.