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

hapi-firebase-auth

v0.2.0

Published

Hapi.js auth strategy using Firebase access tokens

Downloads

97

Readme

Overview

If you have an App using Firebase Auth and need to connect them with your backend API, this is the plugin for you.

This auth strategy verify the token sent in the request and only grant access to valid tokens. Invalid tokens will get a 401 - Unauthorized response.

Features

  • Compatible with Hapi v17
  • Firebase Admin initializer and loader
  • Gluten-free

Instalation

Using NPM

npm install hapi-firebase-auth --save

Using Yarn

yarn add hapi-firebase-auth

Usage

Step 1 - Add auth strategy

Using a new Firebase Admin instance

In case you don't want to initialize Firebase Admin externally, pass your Firebase credentials using the property credential as shown below. This way the plugin will handle it for you.

// Load Hapi-Firebase Auth Strategy
const HapiFirebaseAuth = require('hapi-firebase-auth');

// Register the plugin
await server.register({
  plugin: HapiFirebaseAuth
});

// Include auth strategy
server.auth.strategy('firebase', 'firebase', {
  credential: {
    projectId: '<PROJECT_ID>',
    clientEmail: 'foo@<PROJECT_ID>.iam.gserviceaccount.com',
    privateKey: '-----BEGIN PRIVATE KEY-----\n<KEY>\n-----END PRIVATE KEY-----\n',
    databaseURL: 'https://<DATABASE_NAME>.firebaseio.com'
  }
})

You can get the credentials for your project in your Firebase Console. More details here.

Using a pre-existing Firebase Admin instance

If there is already an existing Firebase Admin instance, pass it using the property instance as shown below.

// Load Hapi-Firebase Auth Strategy
const HapiFirebaseAuth = require('hapi-firebase-auth');

// Initialize the default app
const admin = require('firebase-admin');

// Register the plugin
await server.register({
  plugin: HapiFirebaseAuth
});

// Initialize the Admin SDK with your credentials
// This is an example of what it should look in your code
admin.initializeApp({
  credential: admin.credential.cert({
    projectId: '<PROJECT_ID>',
    clientEmail: 'foo@<PROJECT_ID>.iam.gserviceaccount.com',
    privateKey: '-----BEGIN PRIVATE KEY-----\n<KEY>\n-----END PRIVATE KEY-----\n'
  }),
  databaseURL: 'https://<DATABASE_NAME>.firebaseio.com'
});

// Include auth strategy using existing Firebase Admin instance
server.auth.strategy('firebase', 'firebase', {
  instance: admin
})

If you are having issues with Firebase Admin SDK, click here and make sure all your credentials are correct.

Step 2 - Setup routes

Add property auth with value firebase to the config object in the routes you want to protect.

server.route({ 
  method: 'GET', 
  path: '/',
  config: { 
    auth: 'firebase' 
  },
  handler(request, reply) { 
    return "Can't touch this!" 
  }
});

Step 3 - Test requests

Send requests to the protected endpoints using the authorization header:

Authorization: Bearer ey3tn03g2no5ig0gimt9gwglkrg0495gj(...)
  • If the provided token is VALID, the endpoint will be accessible as usual.
  • If the provided token is INVALID or EXPIRED, a 401 - Unauthorized error will be returned.

Error codes

| Code | Description | |-------------------------------------|-------------------------------------------------------| | token_not_provided | Authorization header with Bearer keyword not found| | auth_provider_not_initialized | Firebase Admin was not initialized properly (check your credentials) | | invalid_token | The token is not valid. It could also be expired. |

Support

24/7 customer service available. You can find the number for your area on the back of this page.

Contribute

Contribuitions are welcome and highly encouraged! This is a simple plugin but we can always make it better ;)