npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

koa-firebase-middleware

v1.2.4

Published

A simple middleware for authentication by Firebase

Downloads

6

Readme

Koa Firebase Middleware

A simple middleware for authentication in Koa 2 with Firebase, This library using the Firebase Admin for verify and authentication from Firebase database.

Requires

  • KOA.js version 2
  • Node.js version 7 or newer (should support async/await or Babel)
  • MongoDB
  • Redis

Install

npm install koa-firebase-middleware

Before usage

Start servers

To use this library you have to start MongoDB and Redis server before, Redis for caching the token and expiry time and MongoDB for store fid and insert a new user.

Implement with Koa

This library is a middleware in Koa.js only support in version 2.

Usage

Will show an example and how to use this library in CommonJS.

  1. You have to require Koa, Koa-router and this lib.
const Koa = require('koa')
const Router = require('koa-router')
const firebaseAuth = require('koa-firebase-middleware')
...
  1. Create new Koa application and using Koa router.
...
const app = new Koa()
const router = new Router()
...
  1. Create initialize for middleware and prepaid datas from Firebase.

Where can i get it? Going to Firebase Admin SDK (https://console.firebase.google.com/project/{YOU_PROJECT_ID}/settings/serviceaccounts/adminsdk)

  • credential is json object get it from Firebase database.
  • databaseURL is url from Firebase database.
...
firebaseAuth.init({
  credential: require('./FIREBASE_ACCESS_KEY.json'),
  databaseURL: 'https://SOME_ID.firebaseio.com'
})
...
  1. Create a router and listen the application with one middleware firebaseAuth.verifyAccessToken and these keys are require in headers.
{
	"Authorization": "ACCESS_TOKEN_FROM_FIREBASE",
	"FID": "FIREBASE_FROM_FIREBASE"
}
  • Authorization is access token from Firebase.
  • FID is unique id from Firebase.
...
router.get('/', firebaseAuth.verifyAccessToken, (ctx, next) => {
  ctx.body = 'Welcome to Firebase Middleware'
})

app
  .use(router.routes())
  .use(router.allowedMethods())

app.listen(3000, () => {
  console.log('listening on port 3000')
})

If you want to use ctx.user that datas from Redis, you just add passUserContext method to be middleware like this:

...
router.get('/users/currently-logged', firebaseAuth.passUserContext, (ctx, next) => {
  ctx.body = 'Welcome to Firebase Middleware'
})
  • The passUserContext method will not checking on Firebase Database and will not throw "Unauthorized"

Example

This an example for basic authorization with Firebase, simple and very easy to use it.

const Koa = require('koa')
const Router = require('koa-router')
const firebaseAuth = require('koa-firebase-middleware')

const app = new Koa()
const router = new Router()

firebaseAuth.init({
  credential: require('./FIREBASE_ACCESS_KEY.json'),
  databaseURL: 'https://SOME_ID.firebaseio.com'
})

router.get('/', firebaseAuth.verifyAccessToken, (ctx, next) => {
  ctx.body = 'Welcome to Firebase Middleware'
})

app
  .use(router.routes())
  .use(router.allowedMethods())

app.listen(3000, () => {
  console.log('listening on port 3000')
})

Customize

this library is support custom to any fields in init option's object, by the way we provide default values for using like this:

{
  credential: null,
  databaseURL: null,
  mongo: {
    url: 'mongodb://localhost:27017/firebase_auth',
    userCollection: 'users',
    fields: {
      authFirebase: 'authFirebase',
      createdAt: 'createdAt',
      fid: 'fid'
    }
  },
  redis: {
    url: 'redis://localhost:6379/0',
    storeKey: 'fid:%(fid)s'
  },
  header: {
    tokenKey: 'Authorization',
    fidKey: 'FID'
  }
}

You can custom any field in above just create an object and put it in init see example below:

firebaseAuth.init({
  credential: require('./key.json'),
  databaseURL: 'https://traova.firebaseio.com',
  mongo: {
    url: 'mongodb://docker:27010/traova',
    userCollection: '_users',
    fields: {
      authFirebase: '_auth_firebase',
      createdAt: '_created_at',
      fid: 'firebase_id'
    }
  }
})

I did custom in mongo section, set url to connect to docker on port 27010, database name "traova", connection to user collection named "_users" and store custom fields.

It should be store into you MongoDB like this:

{
  "_id" : ObjectId("5a1d10cf5b5f4c85e5f2477f"),
  "_auth_firebase": {
    "firebase_id": "AfBru1sf5b5f4c85e5f10Sd9"
  },
  "_created_at": ISODate("2017-11-28T14:31:27.138+0700")
}

License

The MIT License